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

Make None promotable to object, not a subclass, when resolving overloads #3763

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2650,9 +2650,10 @@ def overload_arg_similarity(actual: Type, formal: Type) -> int:
# NoneTyp matches anything if we're not doing strict Optional checking
return 2
else:
# NoneType is a subtype of object
# NoneType can be promoted to object, but isn't actually a subtype (otherwise you
Copy link
Collaborator

Choose a reason for hiding this comment

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

The comment doesn't sound quite right. Maybe something like this: "As a special case, we don't consider NoneType as a subtype of object here, as otherwise you wouldn't be able to define overloads between object and None."

# wouldn't be able to define overloads between object and None).
if isinstance(formal, Instance) and formal.type.fullname() == "builtins.object":
return 2
return 1
Copy link
Collaborator

Choose a reason for hiding this comment

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

Update the docstring above to mention that we treat None -> object as a promotion though it's actually treated as a real subtype elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I forgot that this was true. That makes this seem more hacky.

A general solution (not specific to None or strict-optional) would be to add a fourth level of overload arg similarity for subclasses, e.g.

0: No match
1: Actual can be promoted to formal
2: Actual is a subclass of formal
3: Actual is the same as formal

Would that be preferable? I'd be happy to implement (optionally including refactoring this function to return an Enum) if so. It seems less hacky but much more likely to have unforseen consequences/introduce new errors.

Copy link
Member

Choose a reason for hiding this comment

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

TBH, I was also thinking that three levels of similarity might be not enough, see e.g. #3656. But introducing a new level of similarity is a big change, so that I think we should wait until @JukkaL is back.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Treating promotions as different from other forms of compatibility is already unsafe, and adding more levels of similarity would add more room for unsafety. I'd prefer to use the hack since its effect is limited. Basically we'd only surgically enable a specific use case that we know to be important, but otherwise overload resolution would remain the same.

if isinstance(actual, UnionType):
return max(overload_arg_similarity(item, formal)
for item in actual.relevant_items())
Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-optional.test
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,17 @@ def f(x: int) -> int: pass
reveal_type(f(None)) # E: Revealed type is 'builtins.str'
reveal_type(f(0)) # E: Revealed type is 'builtins.int'

[case testOverloadWithObject]
from foo import *
[file foo.pyi]
from typing import overload
@overload
def f(x: None) -> str: pass
@overload
def f(x: object) -> int: pass
reveal_type(f(None)) # E: Revealed type is 'builtins.str'
reveal_type(f(0)) # E: Revealed type is 'builtins.int'

[case testOptionalTypeOrTypePlain]
from typing import Optional
def f(a: Optional[int]) -> int:
Expand Down