-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
PEP 586: Small wording changes from review; expanded example in Abstract #993
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 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
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -22,9 +22,15 @@ only expressions that have literally the value "4":: | |
def accepts_only_four(x: Literal[4]) -> None: | ||
pass | ||
|
||
accepts_only_four(4) # Ok | ||
accepts_only_four(4) # OK | ||
accepts_only_four(19) # Rejected | ||
|
||
four = 4 | ||
accepts_only_four(four) # Rejected (not a literal) | ||
|
||
four: Final = 4 # See PEP 591 | ||
accepts_only_four(four) # OK if PEP 591 is accepted | ||
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. Although mypy currently doesn't accept this yet. |
||
|
||
Motivation and Rationale | ||
======================== | ||
|
||
|
@@ -76,7 +82,7 @@ This section outlines the baseline behavior of literal types. | |
Core behavior | ||
------------- | ||
|
||
Literal types indicate a variable has a specific and | ||
Literal types indicate that a variable has a specific and | ||
concrete value. For example, if we define some variable ``foo`` to have | ||
type ``Literal[3]``, we are declaring that ``foo`` must be exactly equal | ||
to ``3`` and no other value. | ||
|
@@ -341,8 +347,9 @@ always infer that ``x`` is of type ``str`` in the above example. | |
If type checkers choose to use more sophisticated inference strategies, | ||
they should avoid being too over-zealous while doing so. | ||
|
||
For example, one strategy that does *not* work is always assuming expressions | ||
are Literal types. This naive strategy would cause programs like the | ||
For example, one strategy that does *not* work is always assuming | ||
literal values occurring in expressions have the corresponding Literal | ||
type. This naive strategy would cause programs like the | ||
following to start failing when they previously did not:: | ||
|
||
# If a type checker infers 'var' has type Literal[3] | ||
|
@@ -454,7 +461,7 @@ types. For example, consider ``open``:: | |
@overload | ||
def open(path: _PathType, mode: str) -> IO[Any]: ... | ||
|
||
If we change the signature of ``open`` to use just the first two overloads, | ||
If we were to change the signature of ``open`` to use just the first two overloads, | ||
we would break any code that does not pass in a literal string expression. | ||
For example, code like this would be broken:: | ||
|
||
|
@@ -504,7 +511,7 @@ the above example is essentially pointless: we can get equivalent behavior | |
by using ``S = Literal["foo"]`` instead. | ||
|
||
**Note:** Literal types and generics deliberately interact in only very | ||
basic and limited ways. In particular, libraries that want to typecheck | ||
basic and limited ways. In particular, libraries that want to type check | ||
code containing an heavy amount of numeric or numpy-style manipulation will | ||
almost certainly likely find Literal types as proposed in this PEP to be | ||
insufficient for their needs. | ||
|
@@ -600,8 +607,8 @@ True dependent types/integer generics | |
|
||
This proposal is essentially describing adding a very simplified | ||
dependent type system to the PEP 484 ecosystem. One obvious extension | ||
is to implement a full-fledged dependent type system that let users | ||
predicate types based on their values in arbitrary ways. This would | ||
would be to implement a full-fledged dependent type system that lets users | ||
predicate types based on their values in arbitrary ways. That would | ||
let us write signatures like the below:: | ||
|
||
# A vector has length 'n', containing elements of type 'T' | ||
|
@@ -615,7 +622,7 @@ let us write signatures like the below:: | |
|
||
At the very least, it would be useful to add some form of integer generics. | ||
|
||
Although such a type system would certainly be useful, it’s out-of-scope | ||
Although such a type system would certainly be useful, it’s out of scope | ||
for this PEP: it would require a far more substantial amount of implementation | ||
work, discussion, and research to complete compared to the current proposal. | ||
|
||
|
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.
Potential wording is "Can be rejected" or "May be rejected" to emphasize that type checkers are allowed to reject this (but are not obliged to reject).
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.
But they would be required to reject it if there was intervening code that could possibly assign a different value to
four
. I don't think we need to split such hairs in this initial example.