Skip to content

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
merged 3 commits into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Respond to code review
  • Loading branch information
Michael0x2a committed Nov 15, 2019
commit b4606dc2f295b867beaf81c7182a655cc396a779
28 changes: 17 additions & 11 deletions docs/source/literal_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
..code-block:: python
.. code-block:: python


Expand All @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shorten this comment to say just # Mypy will chose to infer List[int] here

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
***********
Expand Down
2 changes: 1 addition & 1 deletion mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ def analyze_class_attribute_access(itype: Instance,
enum_literal = LiteralType(name, fallback=itype)
# When we analyze enums, the corresponding Instance is always considered to be erased
# due to how the signature of Enum.__new__ is `(cls: Type[_T], value: object) -> _T`
# in Typeshed. However, this is really more of an implementation detail of how Enums
# in typeshed. However, this is really more of an implementation detail of how Enums
# are typed, and we really don't want to treat every single Enum value as if it were
# from type variable substitution. So we reset the 'erased' field here.
return itype.copy_modified(erased=False, last_known_value=enum_literal)
Expand Down
20 changes: 19 additions & 1 deletion test-data/unit/check-literal.test
Original file line number Diff line number Diff line change
Expand Up @@ -2564,7 +2564,7 @@ force4(reveal_type(f.instancevar4)) # N: Revealed type is 'None'
[builtins fixtures/primitives.pyi]
[out]

[case testLiteralFinalErasureInMutableDatastructures]
[case testLiteralFinalErasureInMutableDatastructures1]
# flags: --strict-optional
from typing_extensions import Final

Expand All @@ -2575,6 +2575,24 @@ reveal_type(var1) # N: Revealed type is 'builtins.list[Union[builtins.int, None
reveal_type(var2) # N: Revealed type is 'Tuple[Literal[0]?, None]'
[builtins fixtures/tuple.pyi]

[case testLiteralFinalErasureInMutableDatastructures2]
from typing_extensions import Final, Literal

var1: Final = []
var1.append(0)
reveal_type(var1) # N: Revealed type is 'builtins.list[builtins.int]'

var2 = []
var2.append(0)
reveal_type(var2) # N: Revealed type is 'builtins.list[builtins.int]'

x: Literal[0] = 0
var3 = []
var3.append(x)
reveal_type(var3) # N: Revealed type is 'builtins.list[Literal[0]]'

[builtins fixtures/list.pyi]

[case testLiteralFinalMismatchCausesError]
from typing_extensions import Final, Literal

Expand Down
8 changes: 6 additions & 2 deletions test-data/unit/check-python38.test
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def f(x: int = (c := 4)) -> int:
f(x=(y7 := 3))
reveal_type(y7) # N: Revealed type is 'builtins.int'

reveal_type((lambda: (y8 := 3) and y8)()) # N: Revealed type is 'builtins.int'
reveal_type((lambda: (y8 := 3) and y8)()) # N: Revealed type is 'Literal[3]?'
y8 # E: Name 'y8' is not defined

y7 = 1.0 # E: Incompatible types in assignment (expression has type "float", variable has type "int")
Expand All @@ -240,7 +240,11 @@ def f(x: int = (c := 4)) -> int:
if Alias := int:
z3: Alias # E: Variable "Alias" is not valid as a type

return (y9 := 3) + y9
if reveal_type(y9 := 3) and \ # N: Revealed type is 'Literal[3]?'
reveal_type(y9): # N: Revealed type is 'builtins.int'
reveal_type(y9) # N: Revealed type is 'builtins.int'

return (y10 := 3) + y10

reveal_type(c) # N: Revealed type is 'builtins.int'

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy