Comments (8)
This also came up in the context of Cython. Stefan Behnel's proposal is a function typing.exactly()
, which can be used as for example
from typing import List, exactly
def foo(a: exactly(List[int])): ...
def bar(b: List[exactly(int)]): ...
from typing.
If there is such a facility, it should be used VERY sparingly. It's normally safe to subclass something to add functionality and have your class still work like the parent (imagine a self-sorting subclass of list
- it should JSON-encode like any other list). Too much use of exactly()
would disrupt that - and often in cases where it wouldn't even matter, which would just give type hinting a bad name.
+1 on the concept, but with very clear documentation that this is for the unusual cases. +1 on the name, too - it parallels PyLong_Check
vs PyLong_CheckExact
.
from typing.
I'm wondering if the spelling should be Exactly[t]
rather than exactly(t)
. We don't seem to have a great rule for deciding when to use []
vs. ()
, but in general when something is composable as part of the type system, takes a type (or more) as arguments, and could support issubclass()
or isinstance()
checks, I have proposed []
. Thus Callable[...]
, Tuple[...]
, Union[...]
, and even Optional[...]
, but TypeVar('T')
(the upper case because it's a class instantiation). I think Exactly[...]
fits in this pattern, since the run time representation must retain the information that [Ee]xactly
was used, and it would make sense at run time to use e.g. isinstance(x, Exactly[int])
.
from typing.
Exactly[T] looks good to me.
I think the fact that it's rather verbose is probably enough to discourage unnecesary use.
from typing.
It's a rare case, it might be better not to address it.
from typing.
Another use case (if we decide it's worth it) might be for @overload. Pickling is (conceptually) defined by a function that is overloaded for many types. Several such overloads are for exactly the builtin types (e.g. int, str) and their pickled representation is different from that of subclasses. Still, there are other ways to do this, so if we decide not to include it I won't be upset.
from typing.
@gvanrossum You tagged this "out of scope" an eternity ago. Do you think this is a feasible idea (in which case I would remove the "out of scope" label) or should we close this issue?
from typing.
Just close it.
from typing.
Related Issues (20)
- Specify how type checkers should interpret `__exit__` annotations HOT 2
- Annotations for `__eq__`
- Switch to `main`? HOT 5
- how do I pass down the Class type to a method without using it as a parameter HOT 1
- Spec: Specify how inheriting from Any works HOT 2
- Specify what happens for binary operators (e.g. `__add__`) HOT 3
- Specify NamedTuple HOT 2
- Allow unary `+` in integer Literals HOT 1
- Pass variable as final HOT 4
- Spec: Allow `NoReturn` to appear outside of function return type annotations HOT 6
- 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
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.