You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot assign a default to parameter with type type[T] #1496
This example was brought up in internal chat as well.
This requires an alternative view of default arg types: instead of (or in addition to) viewing it as part of the function signature whose type gets matched against annotated param type at function def sites, we need to handle & match it at every single callsite of the function.
Such a "callsite"-based view of default args does allow more program to pass type checker -- for example the provided snippet won't be able to type check at all if we insist on the "defsite"-based view of default args. On the other hand, the callsite based check is also more expensive to carry out (it needs to happen for every callsite, not once per def site), and also could introduce some interesting behaviors:
# example 0deff[T](x: type[T] =int, y: type[T]) ->T: ... # No type errorreveal_type(f()) # reveals `type[int] | type[str]`# example 1deff[T](x: type[T] =int, y: type[list[T]] =str) ->T: ... # Type error heref() # Another type error which is pretty much a duplicate of the one above
Both Pyrefly and Mypy takes the def-site view currently, and Pyright takes the call-site view.
Regarding #3418, I think that, given the following two rules, we can prove that the default generic assignment is always safe and does not require a call-site-based check.
The generic is attached to an external factor. i.e., the class and not the method
The generic is bound by either a final type or a union of final types
Given these two rules, I can't think of a case that would cause unsound behavior. Whether this is worth implementing is another matter entirely. If you think the rules above are correct, I can poke around and see how hard it would be to implement.
Activity
grievejia commentedon Nov 4, 2025
This example was brought up in internal chat as well.
This requires an alternative view of default arg types: instead of (or in addition to) viewing it as part of the function signature whose type gets matched against annotated param type at function def sites, we need to handle & match it at every single callsite of the function.
Such a "callsite"-based view of default args does allow more program to pass type checker -- for example the provided snippet won't be able to type check at all if we insist on the "defsite"-based view of default args. On the other hand, the callsite based check is also more expensive to carry out (it needs to happen for every callsite, not once per def site), and also could introduce some interesting behaviors:
Both Pyrefly and Mypy takes the def-site view currently, and Pyright takes the call-site view.
mfish33 commentedon May 18, 2026
Regarding #3418, I think that, given the following two rules, we can prove that the default generic assignment is always safe and does not require a call-site-based check.
Given these two rules, I can't think of a case that would cause unsound behavior. Whether this is worth implementing is another matter entirely. If you think the rules above are correct, I can poke around and see how hard it would be to implement.
Replace quantifieds with bounds when validating defaults
Check unmatched params against their defaults in calls