-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Final names and attributes #5522
Conversation
@med-merchise you were asking for this, I think. |
This is super cool! Just two comments -- first, it might be nice if there were some tests + docs about Final and control flow. For example, the following program typechecks without an error: [case testFinalControlFlow]
from typing import Final
for val in [1, 2, 3]:
x: Final = val
[builtins fixtures/list.pyi] I found this a bit surprising since we are rebinding x several times/tools like mypyc can no longer assume that 'x' will have the same value within the loop. But then again, maybe this is ok? I think i'd be sort of hard to accidentally do something weird here, and any weirdness that does happen will be localized since x will be actually final after the loop is over... Not sure, but either way, some clarification might be nice (unless you were planning on doing this in a different PR). Second, I expect that this proposal will be rejected immediately for being too unpythonic + too messy to implemented, but I figure I might as well get it out of the way... How do we feel about adding a flag to mypy that makes all variables and attributes Final by default? |
I would actually prohibit In addition, I just noticed that I forgot about in-place assignments, will fix this soon.
I am not sure I understand this comment. Rejected immediately by whom? There were no objections during PyCon typing meeting, moreover many people liked the idea.
Same story here, sorry :-) Do you want to say that the implementation in this PR is messy?
I am not sure there is any existing Python program larger than 1k lines that will type-check with such flag. Plus we already have dozens of flags. So I am against this unless people will ask often for this. |
Ah, I meant to say that I expected my proposal to add a flag to make final always-on by default would be rejected immediately by you. (Which is what just happened -- but it was worth a shot 😄) |
@JukkaL I implemented only some comments in the docs. I will kill it in the next commit, then you or any one else can finish it whenever. |
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 for the updates! Looks good now, just one minor thing I noticed.
mypy/checker.py
Outdated
base: TypeInfo, base_node: Optional[Node]) -> bool: | ||
"""Check if an assignment overrides a final attribute in a base class. | ||
|
||
This only check situations where either a node in base class is not a variable |
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.
Grammar: check -> checks
OK, all tests passed, we are good to go. Thanks for review! |
This is a runtime counterpart of an experimental feature added to mypy in python/mypy#5522 This implementation just mimics the behaviour of `ClassVar` on all Python/`typing` versions, which is probably the most reasonable thing to do.
This pull request is a follow-up to python#6763: it renames some parameter names and variables that got missed up above. One interesting side-note: it seems like 'Var' notes also contain a `final_value` field that does almost the same thing as this `last_known_value` field, but is ultimately unrelated: it was introduced back in python#5522. I decided to leave this field alone. (I discovered this while updating mypyc and discovered (to my surprise) that nothing broke.)
This pull request is a follow-up to #6763: it renames some parameter names and variables that got missed up above. One interesting side-note: it seems like 'Var' notes also contain a `final_value` field that does almost the same thing as this `last_known_value` field, but is ultimately unrelated: it was introduced back in #5522. I decided to leave this field alone. (I discovered this while updating mypyc and discovered (to my surprise) that nothing broke.)
From python official documentation for Python3.7, I don't see |
@yxliang01 It will be available in |
Thanks for clarification! |
This is a runtime counterpart of an experimental feature added to mypy in python/mypy#5522 This implementation just mimics the behaviour of `ClassVar` on all Python/`typing` versions, which is probably the most reasonable thing to do.
This is a runtime counterpart of an experimental feature added to mypy in python/mypy#5522 This implementation just mimics the behaviour of `ClassVar` on all Python/`typing` versions, which is probably the most reasonable thing to do.
Fixes #1214
Fixes python/typing#286
Fixes python/typing#242 (partially, other part is out of scope)
This is a working implementation of final access qualifier briefly discussed at PyCon typing meeting. Final names/attributes can be used to have more static guarantees about semantics of some code and can be used by other tools like mypyc for optimizations.
We can play with this implementation before starting to write an actual PEP.
The basic idea is simple: once declared as final, a name/attribute can't be re-assigned, overridden, or redefined in any other way. For example:
For more use cases, examples, and specification, see the docs patch.
Here are some comments on decisions made:
Final
for assignments and@final
for decorators. My PEP8-formatted mind just can't accept@Final
:-)...deferred features:
basis. For example:
Final
in function argument types. One argument is simplicity, another is I didn't see many bugs related to shadowing an argument in function bodies, finally people might have quite different expectations for this. If people will ask, this would be easy to implement....and implementation internals:
TypeVar
s in the type of class-level constant, (b) instance-level constant can't be accessed on the class object.Var
s. But I think having two errors occasionally is fine.final_value
for constants initialized with a simple literal, but we never use it. This exists only for tools like mypyc that may use it for optimizations.The PR is open for questions, suggestions, and comments.
cc @ambv @rchen152 @vlasovskikh