mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Class scope name resolution issue

Open hauntsaninja opened this issue 4 years ago • 4 comments

Example courtesy of @gvanrossum , based on https://twitter.com/gvanrossum/status/1354305179244392453

While Python's behaviour is surprising, mypy should match it.

from typing import *

x: Optional[int] = None
y: Optional[int] = None

def f() -> None:
    x = 1
    y = 1
    class C:
        reveal_type(x)  # Incorrectly reveals int, should be Optional[int]
        reveal_type(y)  # Correctly reveals int
        x = 2

I also found https://bugs.python.org/issue24129 interesting. Thanks, ilevkivskyi!

hauntsaninja avatar Jan 29 '21 01:01 hauntsaninja

Hello,

Can you assign this issue to me, please?

Sabine22-alt avatar Oct 09 '25 06:10 Sabine22-alt

we don't assign issues, just open the PR!

hauntsaninja avatar Oct 09 '25 07:10 hauntsaninja

Hi! I’ve been working on this issue, but I’d like to check if I’m on the right track.

At first, I misunderstood where the problem was happening, and I made some changes in scope.py. Later I realized that this file only manages the current target scope, and not the name resolution logic itself.

I’m now exploring checkexpr.py (especially visit_name_expr and analyze_ref_expr) and checker.py, since that seems to be where the actual name resolution and type checking happen.

Could you please confirm if I’m heading in the right direction, or give me any guidance on which part of the code would be best to focus on for this issue?

Thanks a lot ! I’m still getting familiar with mypy’s internals, so any advice would be super helpful

ines-bg avatar Dec 01 '25 08:12 ines-bg

Will need a change in semanal.py I think!

hauntsaninja avatar Dec 01 '25 08:12 hauntsaninja