Skip to content

report unsafe __get__ return types #1175

Done

Description

@KotlinIsland

Describe the Bug

descriptors can be unsafe:

class A: 
    pass

class B(A):
    def __get__(self, *_) -> 1:
        return 1

class C:
    a: A = B()

C.a  # static: A, runtime: 1

we can avoid this by reporting an error on the signature of __get__: "__get__s return type must be assignable to the super classes __get__ (defaulting to the type of the superclass in it's absense)"

the same applies to __set__

Sandbox Link

No response

(Only applicable for extension issues) IDE Information

No response

Activity

  1. added
    help wanted
    good first issue
    and removed
    help wanted
    on Oct 6, 2025
  2. added
    needs-discussion
    and removed
    good first issue
    on Oct 20, 2025
  3. stroxler commented on Oct 20, 2025

    @stroxler
    Contributor

    I think this issue might actually require some careful thought, it's pretty hard to work out the right semantics.

    Several considerations:

    • No existing type checker enforces strict checks here by default, including basedpyright
    • What about the case where the annotation is the descriptor class itself?
    • What if __get__ is actually a complicated override? In particular, what if instance vs class behaviors diverge?
    • What about built-in descriptors, e.g. function types which have method-binding behavior?

    In my mind, it's important to realize descriptors are both underspecified and probably unsound no matter how much work we do. As a result, we need to be pragmatic about what to take on here; we can certainly implement some checks, but I think we need to think things through carefully first.

    And we have much bigger issues with descriptors already because method descriptors (whose behavior is unsound and underspecified) are causing us a lot of problems already. I'm not convinced it's a good idea to add a soundness check until we have some idea how to deal with those more-pressing issues.

  4. stroxler commented on Nov 23, 2026

    @stroxler
    Contributor
  5. self-assigned this
    on Nov 24, 2026
  6. removed
    needs-discussion
    on Nov 24, 2026
  7. stroxler commented on Nov 24, 2026

    @stroxler
    Contributor

    I think it's time to actually try doing this. I tested and it doesn't seem to be that noisy, and descriptor inheritance is a pretty huge soundness gap.

    This doesn't fix the existing (and probably more serious than user-defined classes) problem where some functions are descriptors and some aren't, and the type system can't reliably distinguish them. But it's a big step in the right direction.

    Thanks for filing the issue @KotlinIsland!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions