-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dataclasses, __replace__
and LSP violations
#18216
Comments
This was referenced Nov 30, 2024
Yeah, I agree with your option 1. PR welcome. See also #17623 , we may want to ignore |
jvansanten
added a commit
to AmpelAstro/Ampel-ZTF
that referenced
this issue
Dec 5, 2024
jvansanten
added a commit
to AmpelAstro/Ampel-ZTF
that referenced
this issue
Dec 5, 2024
…173) * fix(deps): update dependency conda-forge/python to v3.13.0 * chore: work around mypy bug in python 3.13 python/mypy#18216 --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Jakob van Santen <[email protected]>
jvansanten
added a commit
to AmpelAstro/Ampel-HU-astro
that referenced
this issue
Dec 6, 2024
jvansanten
added a commit
to AmpelAstro/Ampel-HU-astro
that referenced
this issue
Dec 6, 2024
* fix(deps): update dependency python to v3.13.1 * Loosen implicit astropy requirement via ampel-lsst * Loosen numpy requirement * chore: Update README for a4a57eb * Force update torch * chore: work around mypy bug in python 3.13 python/mypy#18216 --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Jakob van Santen <[email protected]>
khaeru
added a commit
to khaeru/sdmx
that referenced
this issue
Dec 17, 2024
1 task
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In 3.13, the
__replace__
protocol was added in Python, allowing arbitrary classes to define the method so thatcopy.replace
can call it to perform the copy. Along with this feature, dataclasses (and as such, applies to@dataclass_transform()
as well) have the__replace__
method created.Type checkers (at least mypy and pyright) synthesize a
__replace__
method (like the do for__init__
) depending on the defined fields. A common pattern when using dataclasses is to override a field with a more precise type:While this code is technically unsafe, mypy does not raise any error unless you enable the
mutable-override
error code. Note that this error code isn't even enabled in strict mode.However, mypy (if the configured Python version is 3.13 or greater) will emit an error regarding the LSP violation for the synthesized
__replace__
method (playground).I believe this is confusing/annoying for users, because:
__replace__
protocol. It is also confusing to see an error poping about this method when it is not defined by the end user.__replace__
method is synthesized).In contrast, while pyright emits an error for the incompatible field override 1 (essentially doing the same thing as mypy if
mutable-override
were to be enabled), it doesn't raise anything related to the__replace__
LSP violation.In Pydantic, we got a first report about this issue on Oct 24, about two weeks after the final 3.13 release. We also got a similar report today, this time when using aliases. I've raised this discussion a while ago, and commented on the fact that from a practical perspective, this isn't ideal.
Here are a couple ideas to go forward:
__replace__
method.__replace__
method if themutable-override
error code is enabled.I would personally go with option one, especially because while synthesizing a
__replace__
method makes sense, it does not provide any benefit. Users should not call this method directly, and instead usecopy.replace
, which isn't special cased by any type checker as of today (i.e. you don't get any errors if you don't provide the correct arguments to thecopy.replace
function).Footnotes
This was debated at length here. ↩
The text was updated successfully, but these errors were encountered: