Skip to content

Cannot assign a default to parameter with type type[T] #1496

Closed

Activity

  1. grievejia commented on Nov 4, 2025

    @grievejia
    Contributor

    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 0
    def f[T](x: type[T] = int, y: type[T]) -> T: ...  # No type error
    reveal_type(f())  # reveals `type[int] | type[str]`
    
    # example 1
    def f[T](x: type[T] = int, y: type[list[T]] = str) -> T: ... # Type error here
    f()  # 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.

  2. added
    needs-discussionAn issue where it's not clear whether there is a bug or we are behaving as expected.
    on Dec 31, 2025
  3. mfish33 commented on May 18, 2026

    @mfish33
    Contributor

    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.

    1. The generic is attached to an external factor. i.e., the class and not the method
    2. 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.

  4. self-assigned this
    on May 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

needs-discussionAn issue where it's not clear whether there is a bug or we are behaving as expected.typechecking

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions