Skip to content

Enable use-before-def error code by default #14166

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

Merged
merged 49 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
ebe46db
try working, except else
ilinum Oct 28, 2022
e3f708f
all tests passing
ilinum Oct 28, 2022
cc91463
polish
ilinum Oct 28, 2022
dc635f6
formatting
ilinum Oct 28, 2022
2be4ad7
Merge branch 'master' into partial/try
ilinum Nov 16, 2022
702788e
Enable use-before-def error code by default
ilinum Nov 23, 2022
59f59d5
Support imports
ilinum Nov 23, 2022
da367a3
Support function definitions
ilinum Nov 23, 2022
350be89
Merge branch 'master' into partial/try
ilinum Nov 23, 2022
fe439cf
Skip stubs
ilinum Nov 23, 2022
577ca96
Handle builtins
ilinum Nov 23, 2022
0b3882f
Support list unpacking
ilinum Nov 23, 2022
ec1361f
Handle lambdas correctly
ilinum Nov 23, 2022
74adae7
Implicit module attrs
ilinum Nov 23, 2022
66c08a8
Merge branch 'master' into enable-use-before-def
ilinum Nov 25, 2022
15c29a7
Merge branch 'master' into partial/try
ilinum Nov 27, 2022
c0ebc4b
Merge branch 'master' into enable-use-before-def
ilinum Nov 27, 2022
d29966d
Merge branch 'master' into partial/try
ilinum Nov 29, 2022
1354b3c
Fix bug with computing may_be_defined set
ilinum Nov 29, 2022
4c192c5
Only process body twice + better tests
ilinum Nov 29, 2022
3f8b720
Fix is None check
ilinum Nov 29, 2022
071ad7b
[partially defined] fix false positive with global/nonlocal vars
ilinum Nov 29, 2022
9e38e9b
Merge branch 'master' into enable-use-before-def
ilinum Nov 29, 2022
7c0022e
Merge branch 'partial/outer-scope' into enable-use-before-def
ilinum Nov 29, 2022
fe93b76
Merge branch 'partial/try' into enable-use-before-def
ilinum Nov 29, 2022
26e9ec5
Merge branch 'master' into partial/try
ilinum Nov 29, 2022
c1a693f
track definition of exception names
ilinum Nov 29, 2022
9c2a5a5
Merge branch 'partial/try' into enable-use-before-def
ilinum Nov 29, 2022
bbea3e0
Report variables defined in try body inside except as may be undefined
ilinum Nov 30, 2022
6cffcc3
Merge branch 'master' into partial/try
ilinum Nov 30, 2022
bfb7630
Merge branch 'partial/try' into enable-use-before-def
ilinum Nov 30, 2022
be092d6
Merge branch 'master' into enable-use-before-def
ilinum Dec 16, 2022
9cdd8e0
fix check-classes
ilinum Dec 17, 2022
5886eb4
formatting
ilinum Dec 17, 2022
b28e102
fix test-inference
ilinum Dec 17, 2022
284e879
fix check-dynamic-typing
ilinum Dec 17, 2022
13ff33e
Merge branch 'master' into enable-use-before-def
ilinum Dec 19, 2022
8b8a91d
[undefined vars] do not double report errors in class defs
ilinum Dec 27, 2022
a6c32bb
fix several tests
ilinum Dec 27, 2022
c022033
remove unused flag
ilinum Dec 27, 2022
e35b959
[undefined vars] fix per-module error code override bug
ilinum Dec 27, 2022
1802b10
Merge branch 'classbody' into enable-use-before-def
ilinum Dec 27, 2022
73a288c
Merge branch 'undefined/per-module-error-code' into enable-use-before…
ilinum Dec 27, 2022
6dcc9b3
fix remaining tests
ilinum Dec 27, 2022
1c47aae
fix dataclasses test and functions test
ilinum Dec 27, 2022
cd9d26c
that should be all tests now
ilinum Dec 27, 2022
933e174
Merge branch 'master' into enable-use-before-def
ilinum Dec 29, 2022
8cae36c
Merge branch 'master' into enable-use-before-def
ilinum Jan 19, 2023
ed28a7e
rebase
ilinum Jan 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,7 @@ def __str__(self) -> str:
default_enabled=False,
)
USED_BEFORE_DEF: Final[ErrorCode] = ErrorCode(
"used-before-def",
"Warn about variables that are used before they are defined",
"General",
default_enabled=False,
"used-before-def", "Warn about variables that are used before they are defined", "General"
)


Expand Down
8 changes: 4 additions & 4 deletions test-data/unit/check-abstract.test
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,16 @@ class B(A, I): pass

from abc import abstractmethod, ABCMeta

class I(metaclass=ABCMeta):
@abstractmethod
def f(self): pass

o = None # type: object
t = None # type: type

o = I
t = I

class I(metaclass=ABCMeta):
@abstractmethod
def f(self): pass

[case testAbstractClassInCasts]
from typing import cast
from abc import abstractmethod, ABCMeta
Expand Down
79 changes: 26 additions & 53 deletions test-data/unit/check-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,26 @@ class A: pass
class B: pass

[case testConstructionAndAssignment]
x = None # type: A
x = A()
if int():
x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
class A:
def __init__(self): pass
class B:
def __init__(self): pass

x = None # type: A
x = A()
if int():
x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
[case testInheritInitFromObject]
class A(object): pass
class B(object): pass
x = None # type: A
if int():
x = A()
if int():
x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
class A(object): pass
class B(object): pass

[case testImplicitInheritInitFromObject]
class A: pass
class B: pass
x = None # type: A
o = None # type: object
if int():
Expand All @@ -39,10 +40,6 @@ if int():
x = A()
if int():
o = x
class A: pass
class B: pass
[out]

[case testTooManyConstructorArgs]
import typing
object(object())
Expand All @@ -51,21 +48,15 @@ main:2: error: Too many arguments for "object"

[case testVarDefWithInit]
import typing
a = A() # type: A
b = object() # type: A
class A: pass
[out]
main:3: error: Incompatible types in assignment (expression has type "object", variable has type "A")

a = A() # type: A
b = object() # type: A # E: Incompatible types in assignment (expression has type "object", variable has type "A")
[case testInheritanceBasedSubtyping]
import typing
x = B() # type: A
y = A() # type: B # Fail
class A: pass
class B(A): pass
[out]
main:3: error: Incompatible types in assignment (expression has type "A", variable has type "B")

x = B() # type: A
y = A() # type: B # E: Incompatible types in assignment (expression has type "A", variable has type "B")
[case testDeclaredVariableInParentheses]

(x) = None # type: int
Expand Down Expand Up @@ -101,32 +92,22 @@ w = 1 # E: Incompatible types in assignment (expression has type "int", variabl

[case testFunction]
import typing
def f(x: 'A') -> None: pass
f(A())
f(B()) # Fail
class A: pass
class B: pass
[out]
main:4: error: Argument 1 to "f" has incompatible type "B"; expected "A"

def f(x: 'A') -> None: pass
f(A())
f(B()) # E: Argument 1 to "f" has incompatible type "B"; expected "A"
[case testNotCallable]
import typing
A()()
class A: pass
[out]
main:2: error: "A" not callable

A()() # E: "A" not callable
[case testSubtypeArgument]
import typing
def f(x: 'A', y: 'B') -> None: pass
f(B(), A()) # Fail
f(B(), B())

class A: pass
class B(A): pass
[out]
main:3: error: Argument 2 to "f" has incompatible type "A"; expected "B"

def f(x: 'A', y: 'B') -> None: pass
f(B(), A()) # E: Argument 2 to "f" has incompatible type "A"; expected "B"
f(B(), B())
[case testInvalidArgumentCount]
import typing
def f(x, y) -> None: pass
Expand Down Expand Up @@ -194,12 +175,10 @@ main:4: error: Incompatible types in assignment (expression has type "B", variab

[case testVariableInitializationWithSubtype]
import typing
x = B() # type: A
y = A() # type: B # Fail
class A: pass
class B(A): pass
[out]
main:3: error: Incompatible types in assignment (expression has type "A", variable has type "B")
x = B() # type: A
y = A() # type: B # E: Incompatible types in assignment (expression has type "A", variable has type "B")


-- Misc
Expand All @@ -217,15 +196,11 @@ main:3: error: Incompatible return value type (got "B", expected "A")

[case testTopLevelContextAndInvalidReturn]
import typing
def f() -> 'A':
return B()
a = B() # type: A
class A: pass
class B: pass
[out]
main:3: error: Incompatible return value type (got "B", expected "A")
main:4: error: Incompatible types in assignment (expression has type "B", variable has type "A")

def f() -> 'A':
return B() # E: Incompatible return value type (got "B", expected "A")
a = B() # type: A # E: Incompatible types in assignment (expression has type "B", variable has type "A")
[case testEmptyReturnInAnyTypedFunction]
from typing import Any
def f() -> Any:
Expand All @@ -252,6 +227,8 @@ reveal_type(__annotations__) # N: Revealed type is "builtins.dict[builtins.str,


[case testLocalVariableShadowing]
class A: pass
class B: pass
a = None # type: A
if int():
a = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
Expand All @@ -263,10 +240,6 @@ def f() -> None:
a = B()
a = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
a = A()

class A: pass
class B: pass

[case testGlobalDefinedInBlockWithType]
class A: pass
while A:
Expand Down
Loading