Comments (6)
Also, should Var (or typevar) be added to collections.abc, or only to typing?
from typing.
Renamed Var to TypeVar in b7d16de to leave space for variable types in the future.
from typing.
As for the name not being PEP8-compliant, neither is Union, which is also a factory. That's minor.
As for accepting subclasses of constrained types for arguments but limiting return values to explicitly listed classes, that's reasonable but subtle and thus confusing. Seems like it's the covariance discussion in disguise. It's problematic because of this example in the PEP:
X = TypeVar('X')
Y = TypeVar('Y', Iterable[X])
def filter(rule: Callable[[X], bool], input: Y) -> Y:
...
- Since Iterable is abstract, both
input
and the return value need to be subclasses. - Do they have to be the same subclasses? IMHO no, as long as they're both subclassing Iterable.
- More generally, does it have to be true that `issubclass(type(input), type(return_value))? IMHO no, your comment suggests otherwise.
from typing.
I explained the naming/syntax issue in #5 (I hope).
mypy currently doesn't like something like Iterable[X] as a typevar constraint (the 'values=' kwarg). This may be because it doesn't support parametrized types as type aliases; e.g. this fails:
X = typevar('X')
Y = Iterable[X]
I propose that in the first published draft of the PEP we use a simpler example that actually works when translated to mypy syntax, and think more deeply about constraints on type variables a bit later. E.g. this works as an example using Callable and generic types:
X = TypeVar('X')
def filter(pred: Callable[[X], bool], input: Iterable[X]) -> Iterable[X]:
return [x for x in input if pred(x)]
For constrained types it would be better to discuss the AnyStr example in the PEP.
from typing.
TypeVar('X', Constraint1)
is reducible to Constraint1
as Guido pointed out. A type variable with an upper bound makes sense only if the upper bound consists of several constraints: TypeVar('X', Constraint1, Constraint2, ..., ConstraintN)
.
FYI, we have bounded type variables in PyCharm. There are 46 bounded parameterized types out of 1186 typed parameters in our stubs. Most of them are our variant of AnyStr
except for several parameters of the functions of the datetime
module and the filter
built-in function for Python 2.
from typing.
We're agreed on X = TypeVar('X')
. For the constraints, see #2.
from typing.
Related Issues (20)
- 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
- Introduce a `Language` type to provide consistent language information of strings. HOT 5
- Spec: Version and platform checking underspecified HOT 6
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.