Skip to content

Treat Any metaclass the same as Any base class - #13605

Merged
hauntsaninja merged 4 commits into
python:masterfrom
sobolevn:issue-13599
Sep 8, 2022
Merged

Treat Any metaclass the same as Any base class#13605
hauntsaninja merged 4 commits into
python:masterfrom
sobolevn:issue-13599

Conversation

@sobolevn

Copy link
Copy Markdown
Member

Closes#13599

@sobolevn

Copy link
Copy Markdown
MemberAuthor

Also disallow_subclassing_any need to be covered 🤔

Comment threadtest-data/unit/check-classes.test Outdated

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
Copy Markdown
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
Copy Markdown
MemberAuthor

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.

@AlexWaygoodAlexWaygood left a comment

Copy link
Copy Markdown
Member

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?

Comment threadtest-data/unit/check-classes.test Outdated
Comment threadtest-data/unit/check-classes.test Outdated
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>

@AlexWaygoodAlexWaygood left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
MemberAuthor

@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

ifself.options.disallow_subclassing_any:
ifisinstance(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
Copy Markdown
Contributor

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

@hauntsaninjahauntsaninja left a comment

Copy link
Copy Markdown
Collaborator

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:masterSep 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 freeto 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

@sobolevn@hauntsaninja@AlexWaygood