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)
- 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 HOT 1
- Introduce a `Language` type to provide consistent language information of strings. HOT 5
- Spec: Version and platform checking underspecified HOT 6
- Support list comprehension for `TypeVarTuple` HOT 2
- Clarify the float/int/complex special case HOT 5
- Clarify the implications of subclassing Any HOT 1
- Allow multiple `TypeVarTuple` in Generic Classes when they are wrapped. HOT 5
- pytype conformance tests crash if no `python3.11` executable is in `PATH` HOT 5
- Conformance test: make dataclass_hash.py not rely on `typing.Hashable`? HOT 1
- Proposal: Add coerced type narrowing similar to 'cast' HOT 1
- Idenity Type HOT 2
- [spec] clarify assert_type behavior as asserting type equivalence
- [spec] better clarify the difference between a runtime type object and a static type HOT 1
- [spec] a protocol type should be assignable to object? HOT 1
- [spec] define or replace the term "concrete type" HOT 1
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.