Skip to content

Allow non-Any gradual return types for ambiguous overloads - #2350

Open
carljm wants to merge 1 commit into
python:mainfrom
carljm:cjm/gradual-overload-returns
Open

Allow non-Any gradual return types for ambiguous overloads#2350
carljm wants to merge 1 commit into
python:mainfrom
carljm:cjm/gradual-overload-returns

Conversation

@carljm

@carljm carljm commented Aug 27, 2026

Copy link
Copy Markdown
Member

The spec's requirement to infer Any for gradually-ambiguous overload evaluations is a regular source of discontent, as a more precise type could often still meet the graduality requirement that the inferred return type can be gradually used as if it were any of the matched-overload return types.

In fact mypy already does infer more precise types that meet this requirement. For example, mypy combines ambiguous overload returns of list[int] and list[str] into list[Any], which is gradually usable as either list[int] or list[str], and is clearly preferable to Any in its precision.

Adjust the spec language and the conformance suite tests to express the actual graduality requirements, instead of requiring exactly Any.

An inference of Any remains spec-compliant under this change, but more precise gradual types that still meet the graduality requirement can also be spec-compliant.

@carljm carljm added Typing Council decision Needs to be approved by the Typing Council. Do not merge until approved. topic: typing spec For improving the typing spec topic: conformance tests Issues with the conformance test suite labels Aug 27, 2026
@carljm

carljm commented Aug 27, 2026

Copy link
Copy Markdown
Member Author

@jorenham jorenham 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.

I really like this; thanks!

@davidhalter davidhalter 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.

Wow, if I knew it was this simple I would have done that a long time ago. I have preferred Mypy's behavior in this case and I'm happy to reintroduce it in Zuban. I originally was unhappy to remove it, because it feels much better (and I hope this improves Zuban for scipy-stubs and similar cases)...

I still feel that AnyOf might be useful, but this is great for now.

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: conformance tests Issues with the conformance test suite topic: typing spec For improving the typing spec Typing Council decision Needs to be approved by the Typing Council. Do not merge until approved.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants