Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Add some more tests
  • Loading branch information
ilevkivskyi committed Mar 3, 2026
commit 2e9a0b61850a1b3227406bac0816c5826f28da49
33 changes: 33 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -4310,3 +4310,36 @@ def bar(y: float) -> float: ...

foo(1, None, bar) # E: Cannot infer value of type parameter "T2" of "foo"
[builtins fixtures/tuple.pyi]

[case testGlobalVariableNoneInitMultipleFuncs]
x = None

def f() -> None:
global x
reveal_type(x) # N: Revealed type is "None"
x = 1
reveal_type(x) # N: Revealed type is "builtins.int"

def g() -> None:
global x
reveal_type(x) # N: Revealed type is "builtins.int | None"
x = "" # E: Incompatible types in assignment (expression has type "str", variable has type "int | None")
reveal_type(x) # N: Revealed type is "builtins.int | None"

[case testGlobalVariableNoneInitMultipleFuncsRedefine]
# flags: --allow-redefinition-new --local-partial-types

# Widening this is intentionally prohibited (for now).
x = None

def f() -> None:
global x
reveal_type(x) # N: Revealed type is "None"
x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "None")
reveal_type(x) # N: Revealed type is "None"

def g() -> None:
global x
reveal_type(x) # N: Revealed type is "None"
x = "" # E: Incompatible types in assignment (expression has type "str", variable has type "None")
reveal_type(x) # N: Revealed type is "None"
47 changes: 44 additions & 3 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -11557,7 +11557,7 @@ def foo(x: C) -> Optional[int]:
from n import f
class C:
x = None
def foo(self, x: int) -> None:
def foo(self) -> None:
self.x = f()
[file n.py]
def f() -> int: ...
Expand All @@ -11582,7 +11582,7 @@ def dec(fn: F) -> F: ...
class C:
x = None
@dec
def foo(self, x: int) -> None:
def foo(self) -> None:
self.x = f()
[file n.py]
def f() -> int: ...
Expand Down Expand Up @@ -11631,7 +11631,7 @@ def dec(fn: F) -> F: ...

class C:
x = None
def foo(self, x: int) -> None:
def foo(self) -> None:
@dec
def setup() -> None:
self.x = f()
Expand All @@ -11642,3 +11642,44 @@ def f() -> str: ...
[out]
==
main:4: error: Incompatible return value type (got "str | None", expected "int | None")

[case testPartialNoneTypeFineModuleGlobal]
from typing import Optional
import m
def foo() -> Optional[int]:
return m.x
[file m.py]
from n import f

x = None
def foo() -> None:
global x
x = f()
[file n.py]
def f() -> int: ...
[file n.py.2]
def f() -> str: ...
[out]
==
main:4: error: Incompatible return value type (got "str | None", expected "int | None")

[case testPartialNoneTypeFineModuleGlobalNested]
from typing import Optional
import m
def foo() -> Optional[int]:
return m.x
[file m.py]
from n import f

x = None
def foo() -> None:
def bar() -> None:
global x
x = f()
[file n.py]
def f() -> int: ...
[file n.py.2]
def f() -> str: ...
[out]
==
main:4: error: Incompatible return value type (got "str | None", expected "int | None")