Comments (5)
The core issue here is that we're trying to create an add-on to the language without modifying its syntax. Having X = TypeVar('X')
work but not 'Y = TypeVar('X')` is confusing. Seems like we'd really want TypeVar to be a statement in the local scope. We could make it a "statement", e.g.
Var('X')
Var('AnyStr', bytes, str)
However, it would need to inject the name into the local scope which is just as ugly, I guess.
By the way, why does importing type variables not work?
from typing.
I propose to use mypy's rules. Every TypeVar() call creates a unique type variable. The static checker enforces that TypeVar() must always be assigned to a local (or global, depending on the current scope) variable whose name is the same as the string passed as argument. The runtime doesn't enforce this, but it still treats each TypeVar() as a unique type. For example:
T = TypeVar('T')
X = TypeVar('T') # Fails static check, but OK at runtime
Now the following asserts all pass:
assert issubclass(T, T)
assert issubclass(X, X)
assert not issubclass(T, X)
assert not issubclass(X, T)
assert T != X
(Note that the first two simply check that issubclass of a type with itself should always be true.)
from typing.
Oh, my comment about import was simply that mypy will (of course) let you define type vars with the same name in multiple modules, and you can import them, but they will still be considered different. E.g. this passes in mypy:
from typing import AnyStr
def make_url(host: AnyStr, path: AnyStr) -> AnyStr:
if isinstance(host, str):
return 'https://' + host + path
if isinstance(host, bytes):
return b'https://' + host + path
print(make_url('www.python.org', '/dev/peps/'))
I can also systematically substitute typing.AnyStr for AnyStr.
It also works if I replace the import with:
import typing
AnyStr = typing.typevar('AnyStr', values=(str, bytes))
But after that if fails if I replace the return type with typing.AnyStr:
import typing
AnyStr = typing.typevar('AnyStr', values=(str, bytes))
def make_url(host: AnyStr, path: AnyStr) -> typing.AnyStr:
if isinstance(host, str):
return 'https://' + host + path
if isinstance(host, bytes):
return b'https://' + host + path
print(make_url('www.python.org', '/dev/peps/'))
The errors are:
z1.py: In function "make_url":
z1.py, line 5: Incompatible return value type: expected builtins.bytes*, got builtins.str
z1.py, line 7: Incompatible return value type: expected builtins.str*, got builtins.bytes
from typing.
OK, I understand. Will update the PEP accordingly.
from typing.
I updated the PEP at PyCon to describe the new type variable semantics. I think that this can be closed now.
from typing.
Related Issues (20)
- Treat alternative syntax for unions (and other "new-semantics-old-syntax" typing features) in explicit `TypeAlias`es inside `if TYPE_CHECKING` blocks as stringized annotations. HOT 6
- Spec: Annotating the `self` argument in `__init__` methods HOT 17
- FYI: I removed the "stable" docs version
- Pre-draft proposal: Amend the prior removal of support for unpacking of typevartuples in union. (revisiting pep 646) HOT 1
- PR preview HOT 3
- Outdated stub package guidance HOT 1
- Integrate the Type Stubs document into the spec HOT 1
- Types for "truthy" and "falsy" values HOT 6
- Abstract classmethod returning object of type cls: "is not a supertype of its class" HOT 1
- hello
- Spec: Add PEP 696 (Type parameter defaults) HOT 2
- Spec: Add PEP 705 (Read-only TypedDict items) HOT 3
- Extra key of TypedDict HOT 2
- Reason given for disallowing non-concrete subtype assignment is unsound HOT 10
- Out-of-date description of how to indicate positional-only parameters
- Typing spec should be clearer that type checkers are not expected to support PEP 3141 HOT 22
- Incorporate PEP 483 into the spec HOT 5
- Conformance tests: Add mechanism to allow errors on one of multiple lines HOT 2
- Conformance tests: Add mechanism to ignore errors
- Conformance tests: Align automated and manual scoring
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typing.