-
Notifications
You must be signed in to change notification settings - Fork 242
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
Allow specifying a default for omitted type parameters #307
Comments
@kirbyfan64 (btw |
Another though: since classes can take arbitrary arguments: class Base: pass
class Derived(Base): pass
T = TypeVar('T', bound=Base)
class MyType(Generic[T], T=Base):
pass
myvar: MyType = None # type of `myvar` is `MyType[Base]` Then, on the |
+1 Would be so nice to have a |
As far as I remember, for now we can not do type aliases like this either:
because of a syntax error. Is there any way around until the subject is implemented? |
You can just write |
@JelleZijlstra Brilliant! Thanks! |
Another example where this could be beneficial, from typeshed: _T = TypeVar("_T", default=None)
def nullcontext(enter_result: _T = ...) -> ContextManager[_T]: ... At the moment this uses an overload. |
I've written up a draft PEP for this feature if anyone in this thread is still around and wants to give it a look feedback would be appreciated https://gist.github.com/Gobot1234/8c9bfe8eb88f5ad42bf69b6f118033a7 |
Thanks @Gobot1234 for writing up the PEP. Overall, I think it's looking good. A few thoughts... You mention that For TypeVarTuples, PEP 484 is very clear that the
The section "Using other TypeVar-likes as the default" implies that other TypeVars can be used in a "default". I'm strongly opposed to this. This adds significant complexity and opens up lots of questions about type variable scoping rules. It will also likely get in the way of ongoing attempts to improve syntax for type variable declaration. I strongly recommend that you change the PEP to be clear that the There's a small typo in the example you provide under the heading "Function Defaults". The type annotation for I found the phrase "a type variable appearing only once in the signature" ambiguous. Normally "signature" refers to the input and return parameters for a function, but I think you're using it here to refer only to the input parameters. The phrase "the parameter's default" confused me on first read. I think you're referring to the default argument value associated with the parameter, not the default type associated with the parameter's TypeVar annotation. Since there are two "defaults" involved here, a few extra adjectives would help resolve the ambiguity. The statement "If a TypeVar with a default annotates a function parameter: the parameter's default must be specified if it only shows up once in the signature" is unclear to me. Does this apply only in cases where the parameter is annotated with a "bare TypeVar"? I presume it doesn't apply when a TypeVar is used as a type argument, as in I like that the proposed PEP is clarifying where a default argument value can be used with a parameter annotated with a "bare TypeVar". This is a case where type checkers have diverged, and it's an opportunity to get everyone on the same page. Under the subhead "Subscription", the code sample includes the line Supporting defaults for TypeVarTuple and ParamSpec adds quite a bit of complexity to the PEP, and I'm not entirely convinced of the value these provide. I understand the argument for completeness, but maybe it would be better to initially add support for TypeVar only. If and when we find that there's a compelling use case for TypeVarTuple and ParamSpec, it could be added in the future. My intuition is that there will not be a compelling use case for these. Adding support for these now might also complicate ongoing efforts to introduce a simplified syntax for TypeVars. I'm not strongly opposed to including these in the PEP, but my general philosophy when it comes to language features is "keep it simple until it becomes clear that the added complexity is justified". |
Thank you for the feedback yet again Eric.
Ideally I'd like the two to be strictly equivalent and neither would involve Unknowns, but as you've pointed out this wouldn't work for TypeVarTuples so, I've chosen to remove it.
After careful consideration it pains me to agree with you, I've removed this section from the draft. I'd really like to update this to include this feature when HKT is implemented.
Whoops, thank you, fixed that.
Yep, you're correct, I've changed this.
Done.
I've changed the wording to "Defaults for parameters aren't required if other parameters annotated with the same TypeVar already have defaults". So this should apply to all the previously mentioned types, bare TypeVars, type arguments and unions.
No the annotations aren't important, it's just to show the current behaviour in Python. I've attempted to make this slightly clearer, so thank you for pointing this out.
Ok I've removed this section as well. I'll publish the changes tomorrow/today |
This would be so valuable for a project of mine (music21) where our main container (Stream) holds any type of subclass of Music21Object (notes, chords, keys, tempos, etc.) in 99% of the cases, but specifying that all elements are of a certain class is extremely valuable in some cases. Right now, mypy and other type checkers are asking people to specify Hope that this doesn't get stalled so far that it's not in 3.12. :-) |
|
This is PEP 696. |
Whoops. Never mind me. |
This has been specified in PEP 696 which was recently approved. |
Take the following class:
My idea is to allow for some way to specify a default other than any when the parameters are omitted. For example:
or (I know this is invalid syntax, but it's the idea that counts):
The text was updated successfully, but these errors were encountered: