Skip to content
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

Used before def false positive when aliasing builtin definition #14476

Closed
JukkaL opened this issue Jan 20, 2023 · 0 comments · Fixed by #14483
Closed

Used before def false positive when aliasing builtin definition #14476

JukkaL opened this issue Jan 20, 2023 · 0 comments · Fixed by #14483
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code topic-possibly-undefined possibly-undefined error code

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 20, 2023

The first error is a false positive, since range is available in builtins:

_range = range  # error: Name "range" is used before definition

_C = C  # error: Name "C" is used before definition

class range:
    pass

class C:
    pass

This is not reported consistently. In this case there is no error:

_range = range  # No error!

class range:
    pass

I assume the error is only generated when we perform two or more semantic analysis passes.

This is a regression introduced in #14166.

@JukkaL JukkaL added the bug mypy got something wrong label Jan 20, 2023
@AlexWaygood AlexWaygood added the topic-possibly-undefined possibly-undefined error code label Jan 20, 2023
@ilevkivskyi ilevkivskyi added the false-positive mypy gave an error on correct code label Jan 20, 2023
ilinum added a commit that referenced this issue Jan 24, 2023
When doing multiple passes, in the example below, `range` will refer to
current's module range. When doing a single pass, `range` will refer to
`builtins.range`:
```python
_range = range
_C = C  # error: Name "C" is used before definition
class range: pass
class C: pass
```

Instead of looking at the output of semanal to check if a variable is
resolving to a `builtins` package, we can just check if it's part of
builtins module.

Fixes #14476.
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 topic-possibly-undefined possibly-undefined error code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants