-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eliminate some false positives on
reportUninitializedInstanceVariable
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/pyright-internal/src/tests/samples/uninitializedVariableBased1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import final | ||
|
||
# The objective of this test is to try a bunch of ways | ||
# to trick the checker into thinking C.x is initialized | ||
# when it's not initialized. | ||
|
||
|
||
class C: | ||
x: int # should be reported as uninitialized | ||
|
||
def __init__(self) -> None: | ||
self.a_method_called_by_init() | ||
|
||
def foo() -> None: # pyright: ignore[reportUnusedFunction] | ||
self.x = 3 | ||
|
||
def a_method_not_called_by_init(self) -> None: | ||
self.x = 3 | ||
|
||
def a_method_called_by_init(self) -> None: | ||
# reference x before initializing it | ||
self.x += 3 | ||
|
||
|
||
def __init__() -> None: | ||
c = C() | ||
c.x = 3 | ||
|
||
|
||
class D: | ||
def __init__(self) -> None: | ||
c = C() | ||
c.x = 3 | ||
|
||
|
||
@final | ||
class E(C): | ||
def __init__(self) -> None: | ||
super().__init__() | ||
self.x = 3 | ||
|
||
|
||
class F(C): | ||
def __init__(self) -> None: | ||
super().__init__() | ||
self.x = 3 # pyright: ignore[reportUnannotatedClassAttribute] |
8 changes: 8 additions & 0 deletions
8
packages/pyright-internal/src/tests/samples/uninitializedVariableBased2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class C: | ||
x: int # should not be reported as uninitialized | ||
|
||
def __init__(self) -> None: | ||
self.reset() | ||
|
||
def reset(self) -> None: | ||
self.x = 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters