-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add Final
for stdlib constants
#12318
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
Conversation
This comment has been minimized.
This comment has been minimized.
We probably can't merge this with all the mysterious new |
This comment has been minimized.
This comment has been minimized.
That's from an older run, it's not occurring anymore if you look at the latest mypy primer. (I accidentally had some |
This comment has been minimized.
This comment has been minimized.
6e1f5a1
to
abb0a27
Compare
Okay, consolidated the commits fixing PyType related stuff: abb0a27 I there's two issues here (the enum one mentioned above) and another with callables, I'll report both, and once they're fixed this commit can be unwinded. |
This comment has been minimized.
This comment has been minimized.
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.
For whatever reason one of the Literal[X]
-> CONSTANT: Final = X
fixed this issue: c2be29c
Diff from mypy_primer, showing the effect of this PR on open source code: sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/ext/autodoc/importer.py: note: In function "import_object":
+ sphinx/ext/autodoc/importer.py:183:25: error: Cannot assign to final name "TYPE_CHECKING" [misc]
+ sphinx/ext/autodoc/importer.py:189:25: error: Cannot assign to final name "TYPE_CHECKING" [misc]
urllib3 (https://github.com/urllib3/urllib3)
+ src/urllib3/util/ssl_.py:136: error: Cannot assign to final name "OP_NO_COMPRESSION" [misc]
+ src/urllib3/util/ssl_.py:136: note: Error code "misc" not covered by "type: ignore" comment
+ src/urllib3/util/ssl_.py:137: error: Cannot assign to final name "OP_NO_TICKET" [misc]
+ src/urllib3/util/ssl_.py:137: note: Error code "misc" not covered by "type: ignore" comment
+ src/urllib3/util/ssl_.py:140: error: Cannot assign to final name "PROTOCOL_TLS" [misc]
+ src/urllib3/util/ssl_.py:140: note: Error code "misc" not covered by "type: ignore" comment
+ src/urllib3/util/ssl_.py:141: error: Cannot assign to final name "PROTOCOL_TLS_CLIENT" [misc]
+ src/urllib3/util/ssl_.py:141: note: Error code "misc" not covered by "type: ignore" comment
mkdocs (https://github.com/mkdocs/mkdocs)
+ mkdocs/tests/__init__.py:4: error: Cannot assign to final name "_MAX_LENGTH" [misc]
setuptools (https://github.com/pypa/setuptools)
+ setuptools/tests/contexts.py:74: error: Cannot assign to final name "ENABLE_USER_SITE" [misc]
|
It might be good to split this into two PRs: a first PR that makes changes like this, which are arguably no-brainers; we can merge this first PR quickly and easily: - FOO: Literal[42]
+ FOO: Final = 42 And a second PR that makes changes like this, which will probably require more careful review from us: - BAR = 42
+ BAR: Final = 42
- BAZ: int
+ BAZ: Final[int] |
Sounds good - let me split out that first change. |
Use the following script: https://gist.github.com/max-muoto/3bdfd495d4ee48dba99c16513e855983 to add
Final
to all module-level standard library constants. (Used libcst as opposed to the native ast module, since it was the easiest way to preserve comments/formatting details across all modules)Technically this script makes
MY_CONSTANT: Literal[1]
intoMY_CONSTANT: Final[Literal[1]]
, but I ran the Ruff ruleredundant-final-literal
to fix cases of this.Otherwise:
With
TypeVar
,ParamSpec
,TypeVarTuple
, andNewType
excluded.Additionally, ths this excludes the
wintypes.pyi
module (see commit here), adding final leads Pyright to raise aVariable not allowed in type expression
warning. I'll dig a bit deeper post this going out, and possibly put up an issue on Pyright's end, since this initially seems like a bug to me.The other commits on this PR are mostly fixing things like
_TypeVar
that my script didn't account, and removing an extra import ofFinal
intyping.pyi
andtyping_extensions.pyi
.