Skip to content

Conversation

@mzagozen
Copy link
Contributor

@mzagozen mzagozen commented Apr 5, 2022

Since Python 3.6, the replace() methods support subclasses - the return
type is the subclass being replaced.

Link to Python issue: https://bugs.python.org/issue31222

@srittau
Copy link
Collaborator

srittau commented Apr 5, 2022

Thanks, you still need to annotate self with Self, as this is currently simply a type var, not typing.Self from PEP 673.

@github-actions

This comment has been minimized.

Since Python 3.6 the 'replace()' methods support subclasses - the return
type is the subclass being replaced.
@mzagozen mzagozen force-pushed the datetime-replace-return-self branch from c5cf45f to 99eda80 Compare April 5, 2022 19:13
@mzagozen
Copy link
Contributor Author

mzagozen commented Apr 5, 2022

Thanks, you still need to annotate self with Self, as this is currently simply a type var, not typing.Self from PEP 673.

@srittau I just noticed my editor was giving me the same error🤦

@github-actions
Copy link
Contributor

github-actions bot commented Apr 5, 2022

Diff from mypy_primer, showing the effect of this PR on open source code:

pandas (https://github.com/pandas-dev/pandas)
+ pandas/_libs/tslibs/timestamps.pyi:116: error: Return type "datetime" of "replace" incompatible with return type "Timestamp" in supertype "datetime"  [override]
+ pandas/_libs/tslibs/timestamps.pyi:116: error: Signature of "replace" incompatible with supertype "date"  [override]
+ pandas/_libs/tslibs/timestamps.pyi:116: note:      Superclass:
+ pandas/_libs/tslibs/timestamps.pyi:116: note:          def replace(self, year: int = ..., month: int = ..., day: int = ...) -> Timestamp
+ pandas/_libs/tslibs/timestamps.pyi:116: note:      Subclass:
+ pandas/_libs/tslibs/timestamps.pyi:116: note:          def replace(self, year: int = ..., month: int = ..., day: int = ..., hour: int = ..., minute: int = ..., second: int = ..., microsecond: int = ..., tzinfo: Optional[tzinfo] = ..., fold: int = ...) -> datetime

@mzagozen
Copy link
Contributor Author

mzagozen commented Apr 5, 2022

This mypy_primer robot is pretty neat. I guess I should now make the equivalent PR in the pandas repo to fix https://github.com/pandas-dev/pandas/blob/main/pandas/_libs/tslibs/timestamps.pyi#L116?

@JelleZijlstra
Copy link
Member

This mypy_primer robot is pretty neat. I guess I should now make the equivalent PR in the pandas repo to fix https://github.com/pandas-dev/pandas/blob/main/pandas/_libs/tslibs/timestamps.pyi#L116?

That would be great, but it's not required. We use the tool to get a sense of the impact of changes, and sometimes we decide new errors are acceptable and the library will have to fix them.

@srittau srittau merged commit 8ae6781 into python:master Apr 5, 2022
@Akuli
Copy link
Collaborator

Akuli commented Apr 7, 2022

The version checks shouldn't be necessary, because we no longer support Python 3.5.

@srittau
Copy link
Collaborator

srittau commented Apr 7, 2022

@Akuli Good catch, #7602.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants