-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Y052: Disallow assignments to constant values in global or …
…class namespaces where the assignments don't have type annotations (#339) Following #326, we now allow constructs such as these in a stub file: ```python x = 1 y = "foo" z = b"bar" ``` It's good that we now allow variables to have default values. However, the above constructs are problematic in a stub, as we're leaving it up to the type checker to make inferences about the types of these variables, which can have unpredictable consequences. For example, the `x` variable could be inferred to be of type `int`, `Final[int]`, `Literal[1]`, or `Final[Literal[1]]`. In a class namespace, the problem is even worse -- the `eggs` variable in the following snippet could reasonably be inferred as being of type `str`, `ClassVar[str]`, `Final[str]`, `Literal["EGGS"]`, `ClassVar[Literal["EGGS"]]`, or `Final[Literal["EGGS"]]`: ```python class Whatever: eggs = "EGGS" ``` This PR introduces Y052, enforcing type annotations for assignments to simple constants.
- Loading branch information
1 parent
29ba4e3
commit 516317d
Showing
5 changed files
with
125 additions
and
31 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
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
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
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
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