-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Make revealed type of Final vars distinct from non-Final vars #7955
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
Merged
Michael0x2a
merged 3 commits into
python:master
from
Michael0x2a:adjust-final-var-reporting
Nov 15, 2019
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Respond to code review
- Loading branch information
commit b4606dc2f295b867beaf81c7182a655cc396a779
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -125,9 +125,12 @@ you can instead change the variable to be ``Final`` (see :ref:`final_attrs`): | |
expects_literal(c) # ...and this type checks! | ||
|
||
If you do not provide an explicit type in the ``Final``, the type of ``c`` becomes | ||
context-sensitive: mypy will basically try "substituting" the original assigned | ||
value whenever it's used before performing type checking. So, mypy will type-check | ||
the above program almost as if it were written like so: | ||
*context-sensitive*: mypy will basically try "substituting" the original assigned | ||
value whenever it's used before performing type checking. This is why the revealed | ||
type of ``c`` is ``Literal[19]?``: the question mark at the end reflects this | ||
context-sensitive nature. | ||
|
||
For example, mypy will type check the above program almost as if it were written like so: | ||
|
||
.. code-block:: python | ||
|
||
|
@@ -138,9 +141,13 @@ the above program almost as if it were written like so: | |
reveal_type(19) | ||
expects_literal(19) | ||
|
||
In other words, the type of ``c`` is *context-dependent*: It could be either ``int`` | ||
or ``Literal[19]`` depending on where it's used. For example, here is an example of | ||
where mypy will decide to use ``int`` over ``Literal[19]``: | ||
This means that while changing a variable to be ``Final`` is not quite the same thing | ||
as adding an explicit ``Literal[...]`` annotation, it often leads to the same effect | ||
in practice. | ||
|
||
The main cases where the behavior of context-sensitive vs true literal types differ are | ||
when you try using those types in places that are not explicitly expecting a ``Literal[...]``. | ||
For example, compare and contrast what happens when you try appending these types to a list: | ||
|
||
..code-block:: python | ||
|
||
|
@@ -149,19 +156,18 @@ where mypy will decide to use ``int`` over ``Literal[19]``: | |
a: Final = 19 | ||
b: Literal[19] = 19 | ||
|
||
# Mypy will chose to infer List[int] instead of List[Literal[19]?] or | ||
# List[Literal[19]] since the former is most likely more useful. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shorten this comment to say just |
||
list_of_ints = [] | ||
list_of_ints.append(a) | ||
reveal_type(list_of_ints) # Revealed type is 'List[int]' | ||
|
||
# But if the variable you're appending is an explicit Literal, mypy | ||
# will infer List[Literal[19]]. | ||
list_of_lits = [] | ||
list_of_lits.append(b) | ||
reveal_type(list_of_lits) # Revealed type is 'List[Literal[19]]' | ||
|
||
This is why the revealed type of ``c`` is ``Literal[19]?``: the question mark at | ||
the end indicates the context-sensitive nature of ``c``. | ||
|
||
So while changing a variable to be ``Final`` is not quite the same thing as adding | ||
an explicit ``Literal[...]`` annotation, it often leads to the same effect in practice. | ||
|
||
Limitations | ||
*********** | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.