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

Treat Any metaclass the same as Any base class #13605

Merged
merged 4 commits into from
Sep 8, 2022

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Sep 4, 2022

Closes #13599

@sobolevn
Copy link
Member Author

sobolevn commented Sep 4, 2022

Also disallow_subclassing_any need to be covered 🤔


reveal_type(WithMeta.a) # N: Revealed type is "Any"
reveal_type(WithMeta.m) # N: Revealed type is "Any"
reveal_type(WithMeta.x) # N: Revealed type is "builtins.int"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a couple of tests to check/clarify that this only affects class objects, not instances of classes?

Suggested change
reveal_type(WithMeta.x) # N: Revealed type is "builtins.int"
reveal_type(WithMeta.x) # N: Revealed type is "builtins.int"
reveal_type(WithMeta().a) # E: "WithMeta" has no attribute "a" \
# N: Revealed type is "Any"
reveal_type(WithMeta().x) # N: Revealed type is "builtins.int"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

@github-actions

This comment has been minimized.

2 similar comments
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you just do this?

test-data/unit/check-classes.test Outdated Show resolved Hide resolved
test-data/unit/check-classes.test Outdated Show resolved Hide resolved
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel qualified to comment on whether this is the best way to implement this, but the tests look great!

@sobolevn
Copy link
Member Author

sobolevn commented Sep 4, 2022

@AlexWaygood you can see that I was copying the approach we have for base classes:

mypy/mypy/semanal.py

Lines 1894 to 1900 in 9d21615

if self.options.disallow_subclassing_any:
if isinstance(base_expr, (NameExpr, MemberExpr)):
msg = f'Class cannot subclass "{base_expr.name}" (has type "Any")'
else:
msg = 'Class cannot subclass value of type "Any"'
self.fail(msg, base_expr)
info.fallback_to_any = True

Basically, it uses this mechanism:

mypy/mypy/nodes.py

Lines 2738 to 2743 in 9d21615

# If true, any unknown attributes should have type 'Any' instead
# of generating a type error. This would be true if there is a
# base class with type 'Any', but other use cases may be
# possible. This is similar to having __getattr__ that returns Any
# (and __setattr__), but without the __getattr__ method.
fallback_to_any: bool

@github-actions
Copy link
Contributor

github-actions bot commented Sep 4, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@hauntsaninja hauntsaninja merged commit 520b83e into python:master Sep 8, 2022
JukkaL pushed a commit that referenced this pull request Jan 22, 2023
Fixes #14254

This essentially re-implements #13605
in a simpler way that also works in incremental mode. Also I decided to
set `meta_fallback_to_any` in case of errors, to match how we do this
for base classes.
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.

Detect Any used as a metaclass
3 participants