Skip to content
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

Fix inheritance false positives with dataclasses/attrs #12411

Merged
merged 7 commits into from
Mar 22, 2022

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Mar 21, 2022

Multiple inheritance from dataclasses and attrs classes works at runtime,
so don't complain about __match_args__ or __attrs_attrs__ which tend
to have incompatible types in subclasses.

Fixes #12349. Fixes #12008. Fixes #12065.

…sses

Multiple inheritance from dataclasses and attrs classes works at runtime,
so don't complain about `__match_args__` or `__attrs_attrs__` which tend
to have incompatible types in subclasses.

Fixes #12349. Fixes #12008. Fixes #12065.
@github-actions

This comment has been minimized.

@AlexWaygood
Copy link
Member

AlexWaygood commented Mar 21, 2022

Does this also fix the false positive in the following code, which currently prevents typeshed from adding __match_args__ annotations to stdlib/_ast.pyi (discussed here: #12203 (comment))?

class AST:
   __match_args__ = ()

class stmt(AST): ...

class AnnAssign(stmt):
   __match_args__ = ('target', 'annotation', 'value', 'simple')

@JukkaL
Copy link
Collaborator Author

JukkaL commented Mar 21, 2022

Does this also fix the false positive in the following code, ....

No, but it shouldn't be hard to also fix them.

@JukkaL JukkaL marked this pull request as draft March 21, 2022 16:04
@jhance
Copy link
Collaborator

jhance commented Mar 21, 2022

It seems like it is not good that a plugin depends on a magic list in mypy. If it is possible to add a parameter similar to final (name TBD) that indicates the field is special in the same way __slots__ is, then that would be preferable (and also clearer because it doesn't centralize the list of special cases)

JukkaL added a commit that referenced this pull request Mar 22, 2022
Allow subclasses to override `__match_args__` freely, and don't require
`__match_args__` to be final. 

This matches runtime behavior. For example, if `B` subclasses `A`, 
`case A(...)` also matches instances of `B`, using the `__match_args__` 
from `A`.

The issue was brough up by @AlexWaygood in 
#12411 (comment).
@JukkaL JukkaL marked this pull request as ready for review March 22, 2022 14:27
@github-actions
Copy link
Contributor

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

rotki (https://github.com/rotki/rotki)
+ rotkehlchen/chain/ethereum/modules/adex/adex.py:808: error: Unused "type: ignore" comment

steam.py (https://github.com/Gobot1234/steam.py)
- steam/profile.py:204: error: Cannot override final attribute "__match_args__" (previously declared in base class "EquippedProfileItems")  [misc]
- steam/profile.py:204: error: Cannot override writable attribute "__match_args__" with a final one  [misc]
- steam/profile.py:204: error: Definition of "__match_args__" in base class "ProfileInfo" is incompatible with definition in base class "EquippedProfileItems"  [misc]

JukkaL added a commit that referenced this pull request Mar 22, 2022
Allow subclasses to override `__match_args__` freely, and don't require
`__match_args__` to be final. 

This matches runtime behavior. For example, if `B` subclasses `A`, 
`case A(...)` also matches instances of `B`, using the `__match_args__` 
from `A`.

The issue was brough up by @AlexWaygood in 
#12411 (comment).
@JukkaL JukkaL merged commit e0f16ed into master Mar 22, 2022
@JukkaL JukkaL deleted the dataclass-inheritance branch March 22, 2022 16:26
JukkaL added a commit that referenced this pull request Mar 23, 2022
Multiple inheritance from dataclasses and attrs classes works at runtime,
so don't complain about `__match_args__` or `__attrs_attrs__` which tend
to have incompatible types in subclasses.

Fixes #12349. Fixes #12008. Fixes #12065.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants