False positive comparison-overlap for nested function and nested class
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
Nested functions inside methods incorrectly resolve attributes of their first argument relative to the surrounding class.
To Reproduce
class foo:
a: str = "foo"
class bar:
class a:
pass
def test(self) -> None:
def inner1(f: foo, s: str) -> bool:
return f.a == s
def inner2(s: str, f: foo) -> bool:
return f.a == s
https://mypy-play.net/?mypy=latest&python=3.11&flags=strict&gist=4c27f653074cd1c3a48bb16f49ca2168
Expected Behavior
As far as I can tell this should be valid code, even though this rarely happens in practice due to CamelCase vs snake_case naming conventions.
Actual Behavior
inner1 fails to type-check, because the a attribute is incorrectly assumed to be the a class inside bar
main.py:10: error: Non-overlapping equality check (left operand type: "Type[a]", right operand type: "str") [comparison-overlap]
Interestingly, inner2 is fine.
Your Environment
- Mypy version used: 1.2.0
- Mypy command-line flags:
--strict - Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.11
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the example from the issue in mypy with Python 3.11 and --strict, then trace how the nested functions resolve the attribute access in the comparison. The fix is done when inner1 no longer reports a comparison-overlap error while inner2 remains valid and the regression is covered by a test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100