Skip to content

Allow redefinition even if a closure function is created between definitions #10016

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
terencehonles opened this issue Feb 3, 2021 · 1 comment
Labels
bug mypy got something wrong

Comments

@terencehonles
Copy link
Contributor

Bug Report

I was trying to debug why I was getting no-redef errors when I had --allow-redefinition. I had seen #6963 so, I figured that because both the definition and use of the variable were in a loop it was not triggering that condition (variable use). This did not turn out to be the case, and I was looking through similar issues I noticed #7984 and the example given did exhibit what I was trying to do and what I expected to work. I was able to confirm that it was working on the version of mypy I was using and I set to reduce my test case to something that was easy to reproduce. I made sure everything was typed by stubbing in concrete definitions and started to pull pieces of my test case apart.

Quickly and surprisingly I found out that a closure function I had defined was breaking the redefinition, but the enclosed function was unrelated to the variable. Without further ado:

To Reproduce

# mypy: allow-redefinition
from random import random

def f() -> None:
    x = 0

    reveal_type(x)
    print(x)

    def f2() -> float:
        return random()

    x = 'x'
    reveal_type(x)

Expected Behavior

reveal_type 1: int
reveal_type: 2: 'str'

Actual Behavior

note: In function "f":
7: note: Revealed type is 'builtins.int'
13: error: Incompatible types in assignment (expression has type "str", variable has type "int")  [assignment]
        x = 'x'
            ^
t2.py:14: note: Revealed type is 'builtins.int'

Your Environment

  • Mypy version used: 0.800
  • Mypy command-line flags: --warn-unused-configs --allow-redefinition
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]                                                                         
allow_redefinition = true                                                                                                                  
pretty = true                                                                  
show_error_codes = true                                                        
show_error_context = true                                                      
warn_unused_ignores = true                                                     
warn_unreachable = true
  • Python version used: 3.8.7
@terencehonles terencehonles added the bug mypy got something wrong label Feb 3, 2021
@terencehonles
Copy link
Contributor Author

It looks like this is happening because of this

self.reject_redefinition_of_vars_in_scope()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant