8000 Fix incorrect type inference for inherited from Any class by TH3CHARLie · Pull Request #8019 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Fix incorrect type inference for inherited from Any class #8019

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

Merged
merged 5 commits into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2451,6 +2451,14 @@ def lookup_definer(typ: Instance, attr_name: str) -> Optional[str]:
else:
return result

# We finish invoking above operators and no early return happens. Therefore,
# we check if either the LHS or the RHS is Instance and fallbacks to Any,
# if so, we also return Any
if ((isinstance(left_type, Instance) and left_type.type.fallback_to_any) or
(isinstance(right_type, Instance) and right_type.type.fallback_to_any)):
any_type = AnyType(TypeOfAny.special_form)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure if special_form is the right thing here but I'm not totally sure what else to do. from_another_any is correct but digging out the other any is almost certainly not worth bothering.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am also not sure about whether special_form is the right thing to go, but clearly directly using from_another_any with either left_type or right_type here is incorrect. To proper use from_another_any, we need a better way to transform the Instance to an Any

return any_type, any_type

# STEP 4b:
# Sometimes, the variants list is empty. In that case, we fall-back to attempting to
# call the __op__ method (even though it's missing).
Expand Down
21 changes: 21 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -6566,3 +6566,24 @@ class A:

reveal_type(A().y) # N: Revealed type is 'builtins.int'
[builtins fixtures/property.pyi]

[case testOpWithInheritedFromAny]
from typing import Any
C: Any
class D(C):
pass

class D1(C):
def __add__(self, rhs: float) -> D1:
return self

reveal_type(0.5 + C) # N: Revealed type is 'Any'

reveal_type(0.5 + D()) # N: Revealed type is 'Any'
reveal_type(D() + 0.5) # N: Revealed type is 'Any'
reveal_type("str" + D()) # N: Revealed type is 'builtins.str'
reveal_type(D() + "str") # N: Revealed type is 'Any'


reveal_type(0.5 + D1()) # N: Revealed type is 'Any'
reveal_type(D1() + 0.5) # N: Revealed type is '__main__.D1'
0