python / python/mypy

Any is inferred with a certain combination of overloads and generics

Open
#17,331 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

(I couldn't come up with a better title, please fix)

I've got a piece of code where the inferred type is sometimes Any when it shouldn't be, and the difference between getting Any and the correct result doesn't make sense so it looks like a mypy bug to me. I may be wrong though.

The background: I'm trying to refactor and fix the overloads for https://github.com/scrapy/scrapy/blob/2b9e32f1ca491340148e6a1918d1df70443823e6/scrapy/utils/defer.py#L360, I've simplified the overloads and ignored the "overlapping signatures" error per https://stackoverflow.com/questions/60222982/type-annotation-for-overloads-that-exclude-types-something-vs-everything-else but the inferred result of the function works fine only if the input coroutine doesn't return e.g. List[Any]. It doesn't seem to fall back to the second overload either, as that wouldn't result in Any too. Overall it doesn't look like something with my code but maybe I'm missing something.

To Reproduce

https://gist.github.com/mypy-play/29b3eb14ab12db21dc8f52594f40c392

Expected Behavior

I expect the revealed type to always be a Deferred with the same param as the coroutine result type and never Any.

Actual Behavior

It's Any when, it looks like, the argument type has Any in it.

Your Environment

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked mypy-play reproduction and gist, comparing overload resolution and revealed types when the coroutine result contains Any versus a precise type. Check the behavior under mypy 1.10.0 and Python 3.12; done means the revealed result consistently preserves the coroutine result type instead of becoming Any.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.