Replies: 4 comments 4 replies
-
For discussion: here is how runtime Python currently treats this: >>> d = dict[str, int]
>>> d[str, int]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: There are no type variables left in dict[str, int]
>>> e = dict
>>> e[str, int]
dict[str, int] I would interpret the current runtime behaviour as implicitly endorsing Option 3. But, perhaps the runtime behaviour should change. |
Beta Was this translation helpful? Give feedback.
-
I like option 4, and I think I have used both |
Beta Was this translation helpful? Give feedback.
-
Is it too late to revisit this decision? I agree that Option 4 is complicated and inconsistent, so it seems the logical resolution is to prefer the simpler and more predictable Option 2. Note that Option 2 is not just easier to support for tooling authors, but also easier for users to understand and remember. I think it should be relatively straightforward to find and update all instances of bare generics in typeshed. So that should not be a factor in the decision making. |
Beta Was this translation helpful? Give feedback.
-
Yeah, we shouldn't coalesce around "complicated and inconsistent" to avoid telling the user they wrote some nonsense, even when the user is typeshed. |
Beta Was this translation helpful? Give feedback.
-
A recent change in pyright prompted a discussion about the correct interpretation of type aliases that refer to generic classes (or other generic type aliases) but omit the type arguments for that generic class/alias.
Examples include:
There are several ways these statements could be interpreted.
TypeAlias
annotation is present, in which case the missing type arguments should be treated asAny
).Any
.MyList = list
would be interpreted asMyList = list[_T]
.Pyright was previously not treating these consistently. I recently made a change to apply interpretation 2 consistently, but this appears to have broken assumptions in some code. See this PR in typeshed.
Interpretation 3 is problematic because it's not only inconsistent with the way type annotations are interpreted everywhere else, but it also creates some challenging ambiguities. For example, should the generic type alias
MyDict
in the example above have four type parameters or two? The author probably intended two, but how would a type checker know this?I'll add another possible interpretation:
4. We could apply rule 3 only in the case where the RHS consists of a single generic class or alias with no union or subscripts. In all other cases, we'd fall back to rule 2.
Option 4 strikes me as complicated and inconsistent, but it might be the best compromise.
We also need to consider whether the rules apply consistently between an implicit type alias and an explicit (PEP 613)
TypeAlias
.Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions