-
Notifications
You must be signed in to change notification settings - Fork 23
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
Introduce Y052: Disallow assignments to constant values in global or class namespaces where the assignments don't have type annotations #339
Conversation
…class namespaces where the assignments don't have type annotations
@@ -83,7 +83,8 @@ currently emitted: | |||
| Y048 | Function bodies should contain exactly one statement. (Note that if a function body includes a docstring, the docstring counts as a "statement".) | |||
| Y049 | A private `TypedDict` should be used at least once in the file in which it is defined. | |||
| Y050 | Prefer `typing_extensions.Never` over `typing.NoReturn` for argument annotations. This is a purely stylistic choice in the name of readability. | |||
| Y051 | Y051 detect redundant unions between `Literal` types and builtin supertypes. For example, `Literal[5]` is redundant in the union `int \| Literal[5]`, and `Literal[True]` is redundant in the union `Literal[True] \| bool`. | |||
| Y051 | Y051 detects redundant unions between `Literal` types and builtin supertypes. For example, `Literal[5]` is redundant in the union `int \| Literal[5]`, and `Literal[True]` is redundant in the union `Literal[True] \| bool`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only change in this line is a small grammar fix: "detect" -> "detects"
This comment has been minimized.
This comment has been minimized.
The tensorflow hit is two enums here: https://github.com/python/typeshed/blob/307aadc632bd7e80f427ad22ff036ebc53a5df2c/stubs/tensorflow/tensorflow/__init__.pyi#L79-L89 Strictly speaking, these are false positives, since the type of enum members needs to be special-cased by type checkers anyway, and the semantics of enums are well established. However, I also don't think it would do a huge amount of harm to add explicit type annotations: class VariableSynchronization(Enum):
- AUTO = 0
- NONE = 1
- ON_WRITE = 2
- ON_READ = 3
+ AUTO: int = 0
+ NONE: int = 1
+ ON_WRITE: int = 2
+ ON_READ: int = 3
class VariableAggregation(Enum):
- AUTO = 0
- NONE = 1
- ON_WRITE = 2
- ON_READ = 3
+ AUTO: int = 0
+ NONE: int = 1
+ ON_WRITE: int = 2
+ ON_READ: int = 3 The It does this: from netaddr.fbsocket import AF_INET6
family: Final = AF_INET6
|
This comment has been minimized.
This comment has been minimized.
I changed my mind about how acceptable the |
This comment has been minimized.
This comment has been minimized.
This looks good but the enum change you suggest would cause a regression: https://mypy-play.net/?mypy=latest&python=3.10&gist=b2ec11ec1691fec01fd1054d51335357 With enum members defined as |
Though |
This change has no effect on typeshed. 🤖🎉 |
Okay, I added some hacky special-casing for enums so that they're excluded from the new check :( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! At some point we may have to do some consensus-gathering to get aligned on how to annotate enums in stubs.
Following #326, we now allow constructs such as these in a stub file:
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 typeint
,Final[int]
,Literal[1]
, orFinal[Literal[1]]
. In a class namespace, the problem is even worse -- theeggs
variable in the following snippet could reasonably be inferred as being of typestr
,ClassVar[str]
,Final[str]
,Literal["EGGS"]
,ClassVar[Literal["EGGS"]]
, orFinal[Literal["EGGS"]]
:This PR introduces Y052, enforcing type annotations for assignments to simple constants.