Skip to content

Invalid type for redefined variable inside nested function #7984

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

Open
JukkaL opened this issue Nov 20, 2019 · 3 comments
Open

Invalid type for redefined variable inside nested function #7984

JukkaL opened this issue Nov 20, 2019 · 3 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 20, 2019

The revealed type in this example is int, even though it should be str:

# mypy: allow-redefinition

def f() -> None:
    x = 0
    print(x)
    x = 'x'

    def g() -> None:
        reveal_type(x)  # int, but should be str
@JukkaL JukkaL added bug mypy got something wrong priority-0-high false-positive mypy gave an error on correct code labels Nov 20, 2019
@rushabh-v
Copy link

rushabh-v commented Nov 29, 2019

Hey @JukkaL
I looked into it and found out that,
that is not because of the nested function. it is because probably mypy doesn't run the line giving an error. For example if you run below code with mypy,

x = 'x'
reveal_type(x)
x = 0
reveal_type(x)

it will give output:

rev.py:2: note: Revealed type is 'builtins.str'
rev.py:3: error: Incompatible types in assignment (expression has type "int", variable has type "str")
rev.py:4: note: Revealed type is 'builtins.str'
Found 1 error in 1 file (checked 1 source file)

So basically either " x = 0" line has not run or the type of x has not changed because it gave the incompatible type in assignment error.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Dec 2, 2019

When using --allow-redefinition the second reveal_type actually shows builtins.int. This issue is about --allow-redefinition (see the first line of the original code fragment).

@DevilXD
Copy link

DevilXD commented Apr 21, 2020

I've just ran into an issue that's pretty similar to this one, even tho I do not use the allow-redefinition flag.

Here's the minimal code:

def func():
    x: Optional[str] = "xyz"
    if not x:
        return
    reveal_type(x)  # Revealed type is 'builtins.str'

    def func2():
        reveal_type(x)  # Revealed type is 'Union[builtins.str, None]'
        pass

It looks like the second function definition completely ignores the if not x: check, falling back to the originally defined type.
Using Python 3.8.1 and latest master branch, flags used are default VSCode ones + untyped defs:

--follow-imports=silent
--show-column-numbers
--check-untyped-defs
--ignore-missing-imports

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high
Projects
None yet
Development

No branches or pull requests

3 participants