# (When) should we assume callable types are method descriptors?

**URL:** <https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938>\
**Category:** Typing\
**Created:** [May 22, 2025, 8:53pm UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938 "2025-05-22T20:53:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![carljm](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/carljm/32/959_2.png) [@carljm](https://discuss.python.org/u/carljm)\
**Post date:** [May 22, 2025, 8:53pm UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/1 "2025-05-22T20:53:51Z")

</div>

Some common callable objects (functions, lambdas) are also bound-method descriptors. That is, they have a ` __get__ ` method which returns a bound-method object that binds the receiver instance to the first argument (and thus the bound-method object has a different signature, lacking the first argument).

Other callable objects (staticmethod objects, instances of classes with a ` __call__ ` method but no ` __get__ ` method) are not bound-method descriptors. If accessed as class attributes via an instance, they are simply themselves.

Both kinds of objects can inhabit the same `Callable` type. (For example, `def f(x: int) -> int` and `staticmethod(f)` both inhabit the type `Callable[[int], int]`.) Normally, this would suggest that we should not assume that a `Callable` type is a bound-method descriptor, because not all of its inhabitants are.

But the bound-method descriptor behavior violates Liskov substitutability: it is not safe to substitute an object which is a bound-method descriptor for one that is not, or vice versa, even if they are both callable with exactly the same signature. When accessed as attributes, they will behave in incompatible ways.

So this is a soundness hole in the type system, both for subclassing in general (implementing ` __get__ ` on a subclass to return something that isn’t a subtype of `Self` makes it an unsound subclass, but no type checker errors on this) and specifically for `Callable` (a given `Callable` type includes both bound-method descriptor subtypes and non-bound-method-descriptor subtypes, and no matter which behavior we choose for `Callable`, some of those will become unsound subtypes.)

I suspect this underlying unsoundness is not worth fixing at this point; the disruption would outweigh the gain. But type checkers still need some answer to the question “should we consider a Callable type to be a bound-method descriptor?”

The simplest answer would be “Callable implies only ` __call__ `, not ` __get__ ` – assume Callable is not a bound-method descriptor”. Both ty and pyrefly (new in-development type checkers) currently implement this. But neither pyright nor mypy do. Instead, both implement a hybrid heuristic, where a `Callable` type as a class attribute is considered to be a bound-method descriptor if it is explicitly wrapped in `ClassVar`, and not otherwise.

This special treatment of `ClassVar` seems to be specific to `Callable`. Both mypy and pyright will happily model the descriptor protocol on other types assigned as class attributes but not explicitly marked with `ClassVar`.

This `ClassVar` heuristic doesn’t appear to have any particular _correctness_ advantage: it is just as unsound (although in different scenarios) as “assume Callable is always a bound method descriptor” or “assume Callable is never a bound method descriptor”. One possible advantage is that it at least allows the annotation author to spell either possibility (albeit not in a particularly obvious way.)

(Mypy seems to also implement an _additional_ special case, where a ` __call__ ` attribute annotated as `Callable` is _always_ assumed to be a bound method descriptor, unlike other attributes. Pyright does not do this.)

As far as I can tell, the spec and conformance suite are currently silent on this question.

I would be interested to hear perspectives from typing users and developers of type checkers regarding which behavior is desirable here, and why. Is this something we should standardize in the typing spec?

In case the above isn’t clear, I’ve put together some sample code which illustrates many of the relevant scenarios ([ty](https://play.ty.dev/b60ede58-af47-437a-8af0-a492f792095a), [pyrefly](https://pyrefly.org/sandbox/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoDCAhgDYlEBGJApgDSHkDOjAakSPUc9XgPrwJqAKCEBjJoygARao1EgkCGLgBcQqBqgATasCi9eaajAMAKRtRLB6qRjCIpR1FVDAUAVtVEx6YAO4oPC4C1ACUUAC0AHyYKDBqmolQIMYAriAoUACMIjp6WAgk5pbALgBEBGX0AB4uqDDh0bHx6popMOmZ1SLiXJIECZo6cgpKqtKy8orKIFAAvBMj07imoa0avcwAbuy8w1NjIC4EEmwgANoyS4cAuvOLBzOrIomipORUzoTvlDTn5xUqs0bjY4ncFgUSOsoJtGDsQLw3mRfl8Tn0zudiMjPv9AaCYCDgeDMNgoYk7EQYEhRIifp9jnS-gDKvjCfViRSqaIIMYABZgLSmSFrcn2LmIiTw2nYmjHU7sTGM6i4lnA1kcsXUnkwfmC4U9e4EZ5CADEUAAohSqEhGLyoEQoBQuJZUNRYLzKbBEG7RLyvABrHiSIyBEDvODJWSCbzut37UYzU1QBDgZSiMAkKDAXBQHYkVKye3MJBoQJaWKxmESR0CuD0Px+nU8e0oFBgMXUct+JA6pNo5hnVyzNtqPo8EwhUyiAB08eWHGaazHfEnM9hUrnh3xayTABU-TD3pIHOW+iWUDy4pIKJZ-PaUvayDDcCkY4JZiBUnEsG6bx6tkguAANxJkgegOiEMJ+qIgazDw4AgJIYCZA4EZgHoTbQCeUDUNU0YwMe2hgcAPDUHESaMOelLpHQ7qeqgWjUpShZfowYBfuWf5EABuDTlAABy7ZujqnpNlAnLUrwABUSbwkgDgEfeboOG2HblrhRDeCQEZiYwRA8kWlZtigEQSaIubsPJV60EmFCpPg-5xsRpFxMkX5UgZXE8QuN5vKkFjfDKcZgIWqlJoxdioGgqQ2nam4zLmkjGRE8W8VA+7+TYMAAOSSF+JBIIG2l0fgqFJpBvoBs2byZEYpVPuhsYBemICvjA04iGaACS4FeoIESVTBzZ9KkPKSCJ+BIh8NB9YW7Buqp2iTAmuCMDZZo9lA3ZPqYfjgOg2nhPBObIU1PpHi2p7FqWl4EWoYjTlNKKmFkaxrpKuxPZ8L07tUWQMkF-zsqy9xrkqQh-T9EMAEwA9NyrnMDwKg49H0Il9NAw1DSY9fas0DdBsFFowo2FhNh5BbNx4PqliHrZg+DbZmu37Wgh04a1J2ZE2zUXdhZ43WRd09NOZnSvDphGqE9CvQ9Yvrp9SqS6sMs7nA-2BfDKpAkj7Io-L4Pq8r0vZGscCw5rKLa6yIMLDO8to+LKJCObxuq51Al3jQOWSCQYBgP64nRmBTFkBGnoqe2zHlgABgYU0GDH9owDACh2TAsj3XkMIQLwkLFFY5RYiQACyfICkCtSLpEMT1IMGjtJ0UDdGI1bF2XOoCvX+jiwYcNW8y7wd7qOtgrbOd56SuS6DnNL5xYhdQBUQ-l1o-asOwld1HEjS13E3eNxkzc9G3K+d2v8pHNC8fvH3DDogqxcD8vZDDwK69nKPBLqqDEBz1PQhs7ckYJPQoBdShL3bqvAAypqUQW9q5NDrtCQ+XQT59E1m-LQsDKTUm7jfMgd8n44kHq-GBcCv5sjBPcMy2pdRTggCA-UgCZ7clEEw0k4Ci5n11B-dgOCuQIPqLvZoB80hHxbrCTBq8+EgAEXg6+vdeBygfhcYhTIX6lxkZfeR8CbZEhoXAuhAoGHsNASQHcZoACCaAiCoE4A1DCfoArzWfK1Lw+B3xuW-J5ag-5AIgCArNKCVUQBJmOohWIjE3gZxbFAVi7EUDlnqDwFMxhcHIXulA8+qwobcn7iQ3W1CFjZPoW9CA2NSnv0vrk2WbCClMiKQSQ0PDqmqOeGwyprTsFwNyVUnpuDRAq1NmIRhDSEaaKwboyh49+m6I6YwyW3T5km1liIfpsiVlLLIefTZvTVnlPYeMlU3S9mDJmcjEppydH7NGew7ZWjdk3MGcM2WQA), [mypy](https://mypy-play.net/?mypy=latest&python=3.12&gist=2935159a8183d1929c8a78b1e586059b), [pyright](https://pyright-play.net/?strict=true&code=GYJw9gtgBALgngBwJYDsDmUkQWEMoDCAhgDYlEBGJApgDSHkDOjAakSPUc9XgPrwJqAKCEBjJoygARao1EgkCGLgBcQqBqgATasCi9eaajAMAKRtRLB6qRjCIpR1FVDAUAVtVEx6YAO4oPC4C1ACUUAC0AHyYKDBqmolQIMYAriAoUACMIjp6WAgk5pbALgBEBGX0AB4uqDDh0bHx6popMOmZ1SLiXJIECZo6cgpKqtKy8orKIFAAvBMj07imoa0avcwAbuy8w1NjIC4EEmwgANoyS4cAuvOLBzOrIomipORUzoTvlDTn5xUqs0bjY4ncFgUSOsoJtGDsQLw3mRfl8Tn0zudiMjPv9AaCYCDgeDMNgoYk7EQYEhRIifp9jnS-gDKvjCfViRSqaIIMYABZgLSmSFrcn2LmIiTw2nYmjHU7sTGM6i4lnA1kcsXUnkwfmC4U9e4EZ5CADEUAAohSqEhGLyoEQoBQuJZUNRYLzKbBEG7RLyvABrHiSIyBEDvODJWSCbzut37UYzU1QBDgZSiMAkKDAXBQHYkVKye3MJBoQJaWKxmESR0CuD0Px%2BnU8e0oFBgMXUct%2BJA6pNo5hnVyzNtqPo8EwhUyiAB08eWHGaazHfEnM9hUrnh3xayTABU-TD3pIHOW%2BiWUDy4pIKJZ-PaUvayDDcCkY4JZiBUnEsG6bx6tkguAANxJkgegOiEMJ%2BqIgazDw4AgJIYCZA4EZgHoTbQCeUDUNU0YwMe2hgcAPDUHESaMOelLpHQ7qeqgWjUpShZfowYBfuWf5EABuDTlAABy7ZujqnpNlAnLUrwABUSbwkgDgEfeboOG2HblrhRDeCQEZiYwRA8kWlZtigEQSaIubsPJV60EmFCpPg-5xsRpFxMkX5UgZXE8QuN5vKkFjfDKcZgIWqlJoxdioGgqQ2nam4zLmkjGRE8W8VA%2B7%2BTYMAAOSSF%2BJBIIG2l0fgqFJpBvoBs2byZEYpVPuhsYBemICvjA04iGaACS4FeoIESVTBzZ9KkPKSCJ%2BBIh8NB9YW7Buqp2iTAmuCMDZZo9lA3ZPqYfjgOg2nhPBObIU1PpHi2p7FqWl4EWoYjTlNKKmFkaxrpKuxPZ8L07tUWQMkF-zsqy9xrkqQh-T9EMAEwA9NyrnMDwKg49H0Il9NAw1DSY9fas0DdBsFFowo2FhNh5BbNx4PqliHrZg%2BDbZmu37Wgh04a1J2ZE2zUXdhZ43WRd09NOZnSvDphGqE9CvQ9Yvrp9SqS6sMs7nA-2BfDKpAkj7Io-L4Pq8r0vZGscCw5rKLa6yIMLDO8to%2BLKJCObxuq51Al3jQOWSCQYBgP64nRmBTFkBGnoqe2zHlgABgYU0GDH9owDACh2TAsj3XkMIQLwkLFFY5RYiQACyfICkCtSLpEMT1IMGjtJ0UDdGI1bF2XOoCvX%2BjiwYcNW8y7wd7qOtgrbOd56SuS6DnNL5xYhdQBUQ-l1o-asOwld1HEjS13E3eNxkzc9G3K%2Bd2v8pHNC8fvH3DDogqxcD8vZDDwK69nKPBLqqDEBz1PQhs7ckYJPQoBdShL3bqvAAypqUQW9q5NDrtCQ%2BXQT59E1m-LQsDKTUm7jfMgd8n44kHq-GBcCv5sjBPcMy2pdRTggCA-UgCZ7clEEw0k4Ci5n11B-dgOCuQIPqLvZoB80hHxbrCTBq8%2BEgAEXg6%2BvdeBygfhcYhTIX6lxkZfeR8CbZEhoXAuhAoGHsNASQHcZoACCaAiCoE4A1DCfoArzWfK1Lw%2BB3xuW-J5ag-5AIgCArNKCVUQBJmOohWIjE3gZxbFAVi7EUDlnqDwFMxhcHIXumaXc3oQlDUiQ2akdoRoGQxnNB8i1aaSCZlAFmyE2ZwDWGaCJrhubOOcCIKB59VhQ25P3EhutqELC6fQt6EBsYjPfpfHpss2H9KZIMgkhoeFTNUc8Nh2Nsm5MGrBapvIinE1JhTeG1M4zLXnNUpAO09r1MOuEzmsxTo8w6UISZ2C4E9LebolWpsxCMPmQjTRWDdGUPHl8j5YzGCSxWe83Bogfmy06TC2R3zQjQrIefFFEL3ZsMYAClUyKdEUP0XrYZhLVGor%2Bew9FWjMVErhQitYQA)). The sample code is written to ensure that everything it does is correct at runtime, but due to the underlying unsoundness described above, every type-checker mis-understands the runtime behavior in some cases.

```py
from typing import Callable, ClassVar, assert_type

class Descriptor:
    def __get__ (self, instance: object, owner: type) -> int:
        return 1

def impl(self: "C", x: int) -> int:
    return x

class C:
    descriptor: Descriptor = Descriptor()
    classvar_descriptor: ClassVar[Descriptor] = Descriptor()

    callable: Callable[["C", int], int] = impl
    classvar_callable: ClassVar[Callable[["C", int], int]] = impl
    static_callable: Callable[["C", int], int] = staticmethod(impl)
    static_classvar_callable: ClassVar[Callable[["C", int], int]] = staticmethod(impl)

c = C()

# Establish a baseline that type checkers generally respect the descriptor
# protocol for values assigned in the class body, whether annotated with
# ClassVar or no:
assert_type(c.descriptor, int)
assert_type(c.classvar_descriptor, int)

# The calls and assignments below are all correct per runtime behavior;
# if a type checker errors on any of them and expects a different
# signature, that indicates unsound behavior. Note that the static_*
# variants are annotated exactly the same as the non-static variants,
# but have different runtime behavior, because Callable does not
# distinguish descriptor vs non-descriptor. Thus, it's unlikely that any
# type checker can get all of these correct.

# If a type-checker assumes that callable types are not descriptors,
# it will (wrongly) error on these calls and assignments:

c.callable(1)
c.classvar_callable(1)

x1: Callable[[int], int] = c.callable
x1(1)
x2: Callable[[int], int] = c.classvar_callable
x2(1)

# If a type-checker assumes that callable types are descriptors,
# it will (wrongly) error on these calls and assignments:

c.static_callable(C(), 1)
c.static_classvar_callable(C(), 1)

y1: Callable[["C", int], int] = c.static_callable
y1(C(), 1)
y2: Callable[["C", int], int] = c.static_classvar_callable
y2(C(), 1)

# Now let's look specifically at annotated ` __call__ ` attributes:

def cm_impl(self: "CallMethod", x: int) -> int:
    return x

class CallMethod:
    __call__ : Callable[["CallMethod", int], int] = cm_impl

def cmc_impl(self: "CallMethodClassVar", x: int) -> int:
    return x

class CallMethodClassVar:
    __call__ : ClassVar[Callable[["CallMethodClassVar", int], int]] = cmc_impl

def cms_impl(self: "CallMethodStatic", x: int) -> int:
    return x

class CallMethodStatic:
    __call__ : Callable[["CallMethodStatic", int], int] = staticmethod(cms_impl)

def cmcs_impl(self: "CallMethodClassVarStatic", x: int) -> int:
    return x

class CallMethodClassVarStatic:
    __call__ : ClassVar[Callable[["CallMethodClassVarStatic", int], int]] = staticmethod(cmcs_impl)

# Again, all of these are correct per runtime behavior; type checker
# errors indicate an unsound interpretation:

# Type checkers which assume callables are not descriptors will (wrongly)
# error on these:

CallMethod()(1)
cm: Callable[[int], int] = CallMethod()
cm(1)

CallMethodClassVar()(1)
cmc: Callable[[int], int] = CallMethodClassVar()
cmc(1)

# Type checkers which assume callables are descriptors will (wrongly)
# error on these:

CallMethodStatic()(CallMethodStatic(), 1)
cms: Callable[["CallMethodStatic", int], int] = CallMethodStatic()
cms(CallMethodStatic(), 1)

CallMethodClassVarStatic()(CallMethodClassVarStatic(), 1)
cmcs: Callable[["CallMethodClassVarStatic", int], int] = CallMethodClassVarStatic()
cmcs(CallMethodClassVarStatic(), 1)

```

---

<div class="post-metadata">

**Author:** ![oscarbenjamin](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/oscarbenjamin/32/1209_2.png) [@oscarbenjamin](https://discuss.python.org/u/oscarbenjamin)\
**Post date:** [May 22, 2025, 10:49pm UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/2 "2025-05-22T22:49:10Z")

</div>

It doesn’t sound like you are proposing to fix this but the situation where I frequently run into the descriptor vs callable inconsistency is with the following that all type checkers incorrectly reject:

```python
class A:
    abs = abs

a = A()

print(a.abs(-10))

```

---

<div class="post-metadata">

**Author:** ![erictraut](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erictraut/32/15190_2.png) [@erictraut](https://discuss.python.org/u/erictraut)\
**Post date:** [May 22, 2025, 10:52pm UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/3 "2025-05-22T22:52:12Z")

</div>

Thanks for starting this discussion. This is a complex topic, and I welcome clarity and standardization in this area.

The typing spec is pretty clear that the `Callable` special form describes a structural type that has a ` __call__ ` method. In other words, it’s comparable to a protocol class with a ` __call__ ` method. No other methods (such as ` __get__ `) are implied.

Internally, pyright models function types with a set of flags that differentiate between normal functions, static methods, class methods, and instance methods. It uses these flags to determine the behavior when binding an object (either a class object or instance) to an attribute or method.

The challenge with attributes that contain callable objects (or more generally, descriptor objects) is that their behavior can change depending on whether the value has been stored on the class object or an instance.

You mentioned that pyright and mypy use a heuristic based on `ClassVar`, and this approach has no correctness benefit. I can’t speak for mypy, but in the case of pyright, the behavior you see is rooted in how it treats [class and instance variables](https://microsoft.github.io/pyright/#/type-concepts-advanced?id=class-and-instance-variables). If an attribute is annotated as a `ClassVar`, pyright considers it a “pure class variable” and enforces that it cannot be set on an instance. The behavior of a descriptor or a callable can be statically determined in this case. When an attribute is not annotated as a `ClassVar`, pyright assumes that it’s OK for someone to set the value on an instance. The runtime behavior is then unknowable because it depends on state that cannot generally be known statically.

This can be demonstrated by extending the sample code that you posted above.

```python
c.classvar_callable(1) # OK (no possibility of false positive or false negative)
c.classvar_callable = impl # Assignment not allowed because classvar_callable is a ClassVar

c.callable(1) # Type error but no crash (false positive)
c.callable(C(), 1) # No type error but crashes (false negative)
c.callable = impl # Assignment allowed because callable is not a ClassVar
c.callable(1) # Type error and crash
c.callable(C(), 1) # No type error, no crash

```

This issue is related to the [recent conversation](https://discuss.python.org/t/differentiating-between-initialized-and-uninitialized-assignments-on-class-toplevel/91172) we had about differentiating between class and instance variables. The challenge stems from the fact that Python allows an instance variable to shadow a class variable of the same name. Most languages require that a class-scoped symbol is _either_ a class variable _or_ an instance variable, not both.

One way to guarantee soundness is to completely disallow shadowing in typed Python, but I think there would be a lot of pushback since this practice is very common. Perhaps we could devise a less draconian restriction that preserves soundness in most (all?) cases. The type system already imposes the limitation that when an instance variable shadows a class variable, the type must be the same for both. For example, you can’t declare the type of a class variable to be a `str` but then indicate that it should be shadowed by a instance variable of type `int`. Developers generally find that restriction to be reasonable. Since some types (notably, some descriptors and callables) behave differently when stored on the class object and the instance, maybe we should disallow shadowing specifically for these categories of types?

In any case, progress in this area probably requires us to first formalize the concepts of “pure class variables”, “pure instance variables” and “class variables that can be shadowed by instance variables” in the typing spec.

---

<div class="post-metadata">

**Author:** ![beauxq](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/beauxq/32/15042_2.png) [@beauxq](https://discuss.python.org/u/beauxq)\
**Post date:** [May 22, 2025, 11:24pm UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/4 "2025-05-22T23:24:18Z")

</div>

Based on what Eric points out, if it’s annotated without `ClassVar`, that means it needs to be able to be treated as an instance variable.

So this assignment should be rejected, because you can’t call it with that signature from an instance.

```python
class C:
    callable: Callable[["C", int], int] = impl

```

This assignment should be accepted, because it can be called with this signature from an instance.

```python
class C:
    callable: Callable[["C", int], int]

    def __init__ (self) -> None:
        self.callable = impl

```

---

<div class="post-metadata">

**Author:** ![xmw](https://avatars.discourse-cdn.com/v4/letter/x/aca169/32.png) [@xmw](https://discuss.python.org/u/xmw)\
**Post date:** [May 23, 2025, 12:13am UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/5 "2025-05-23T00:13:35Z")

</div>

I agree with @erictraut, solving the issue of correctly formalising and inferring pure class variables vs. pure instance variables vs. mixed state will go a long way, doing this will cause most other issues with descriptor types (not just `Callable`s) to be naturally solved without having to add more and more heuristics.

* * *

The runtime behaviour of descriptors (including function definitions) are very clear:

- ` __get__ ` only activates if the item that implements it is a class variable, and has no effect if it’s an instance variable;

```python
class D:
    def __get__ (self, instance, owner):
        raise RuntimeError

class A:
    cd = D()
    def __init__ (self):
        self.id = D()

a = A()
a.id # OK
a.cd # RuntimeError

```

- Data descriptors (items which implement ` __set__ ` and/or ` __delete__ `) will always activate (if the item is a class variable) upon assignment in an instance:

```python
class D:
    def __delete__ (self, obj): ...

class A:
    d = D()

a = A()
a.d = 1 # AttributeError: __set__

```

- Non-data descriptors can be shadowed in an instance:

```python
class A:
    def f(self): ...
    def __init__ (self):
        self.f = 1

a = A()
print(a.f) # 1

```

It follows that all function definitions in a class are implicitly `ClassVar`, because they activate ` __get__ ` to return a bound method when accessed through an instance. Shadowing a function definition via assignment in an instance (`self.f = <r>`) also has a well-defined behaviour at runtime (because functions are not data descriptors), and is actually type-safe if `<r>` is a `Callable` which matches the signature of the bound method, but type-checkers prevent the shadowing due to the same reasoning as `f` being a pure `ClassVar` - you shouldn’t assign a class variable via an instance, even if the types match.

`Callable`s are then method descriptors if they are intended to behave the same as a function definition in the class body. This means, for an assignment-like statement `a` in a class body:

- `a: ClassVar[Callable]` is currently unambiguously a method descriptor, because `Callable` is currently the canonical replacement for the type of a function definition, and as established above, function definitions are implicitly `ClassVar`;
- `a: Callable` where `a` is in ` __slots__ = ("a",)` is definitely _not_ a method descriptor;
- `self.a: Callable` is also definitely _not_ a method descriptor;
- `a = lambda *args, **kwargs: None` is a method descriptor at runtime, but whether a static type checker should treat it as such is dependent on whether they think unannotated assignments are `ClassVar`.

The remaining points of contention is then the interpretation of the following (the second example is chosen to deliberately crash when called via an instance):

- `a: Callable[[], None]`
- `a: Callable = lambda: None`

These points of contention circles back to the ambiguity of class-vs-instance variables for annotations on a class body.

* * *

I think the issue can be considered solved when `classmethod`, `staticmethod`, etc. aren’t special-cased, but expressed purely through type annotations. IMO there is no reason for this code to fail type-checking, but only pyright seems to pass at the moment (and its passing is dependent on the presence of ` __slots__ `):

```python
class A:
    __slots__ = ("m",)
    m: staticmethod[..., int]

a = A()
m: staticmethod[..., int] = a.m # Most type-checkers error here

```

---

<div class="post-metadata">

**Author:** ![carljm](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/carljm/32/959_2.png) [@carljm](https://discuss.python.org/u/carljm)\
**Post date:** [May 23, 2025, 1:09am UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/6 "2025-05-23T01:09:22Z")

</div>

> [@oscarbenjamin](#):
>
> ```python
> class A:
> abs = abs
> 
> ```

The problem here is that `abs` is a builtin function (implemented in C), and this is another kind of callable object that (unlike Python-defined functions) doesn’t implement ` __get__ ` to make itself a bound-method descriptor. But type checkers have to rely on type stubs, and in typeshed `abs` is simply defined as a Python function, so type checkers assume it is a bound-method descriptor.

I think it might be possible to reflect this in the stubs in some way; a hacky way that would likely work would be to simply mark builtin functions with the `@staticmethod` decorator. But I think it’s a separate issue from the topic of this thread.

---

<div class="post-metadata">

**Author:** ![carljm](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/carljm/32/959_2.png) [@carljm](https://discuss.python.org/u/carljm)\
**Post date:** [May 23, 2025, 1:54am UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/7 "2025-05-23T01:54:52Z")

</div>

Thanks for the replies!

> [@erictraut](#):
>
> The typing spec is pretty clear that the `Callable` special form describes a structural type that has a ` __call__ ` method. In other words, it’s comparable to a protocol class with a ` __call__ ` method. No other methods (such as ` __get__ `) are implied.

I agree. I believe that this is the interpretation that ty and pyrefly already implement. If we were to agree on this interpretation, I think it would indicate a change in the current behavior of pyright and mypy: they should not interpret Callable-annotated attributes as bound-method descriptors, regardless of ClassVar. The question of “pure class variables” vs “class-and-instance variables which might be shadowed on the instance” (while certainly important in general) is irrelevant to the topic of this thread, if we don’t consider a Callable type to implement the descriptor protocol in the first place, because a non-descriptor doesn’t behave any differently when set on the class vs on an instance. (Note, though, that agreeing on this doesn’t fix the unsoundness, it just means we agree on one particular version of the unsoundness. I don’t see any way to fix the unsoundness without splitting Callable into two separate incompatible types, which I don’t think is very palatable.)

> [@erictraut](#):
>
> One way to guarantee soundness is to completely disallow shadowing in typed Python

I don’t think the unsoundness here depends on the static unknowability of instance-level shadowing, and I don’t agree that forbidding instance-level shadowing would provide soundness. The code example in my post demonstrates this. If we forbid instance-level shadowing, it remains true that both `impl` and `staticmethod(impl)` are assignable to `Callable[[int], int]`, but at runtime they behave in incompatible ways. Given these two attributes:

```py
def impl(x: int) -> int:
    return x

class C:
    is_a_descriptor_at_runtime: ClassVar[Callable[[int], int]] = impl
    is_not_descriptor_at_runtime: ClassVar[Callable[[int], int]] = staticmethod(impl)

```

Both of those assignments are valid. There is no possibility of instance-level shadowing here, because we are using `ClassVar`. (We could give the same example without `ClassVar` in a hypothetical world where we’ve outlawed instance-level shadowing of class attributes.)

Is a type checker supposed to treat those two attributes differently, despite them being annotated identically? If it does not, then it will definitely be unsound on one of them. (And we can follow up with valid assignments `C.is_a_descriptor_at_runtime = staticmethod(impl)` or `C.is_not_descriptor_at_runtime = impl` to easily invalidate whatever inferred distinctions the type checker might try to make in order to be sound.)

Our uncertainty (and unsoundness) about whether it is correct to treat a Callable type as a descriptor, or not, is inherent in the fact that we allow both runtime descriptors and runtime non-descriptors to be assigned to the same Callable type. It doesn’t require the possibility of instance-level shadowing.

---

<div class="post-metadata">

**Author:** ![TeamSpen210](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/teamspen210/32/1004_2.png) [@TeamSpen210](https://discuss.python.org/u/TeamSpen210)\
**Post date:** [May 23, 2025, 2:50am UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/8 "2025-05-23T02:50:11Z")

</div>

Another point of divergence is attributes - ` __doc__ `, ` __code__ ` etc. Usage of those are rare enough that it’s probably fine to ignore, but it does show another issue. `Callable` feels like it’s a bunch of types in a trenchcoat, but it seems difficult to break up now.

---

<div class="post-metadata">

**Author:** ![erictraut](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erictraut/32/15190_2.png) [@erictraut](https://discuss.python.org/u/erictraut)\
**Post date:** [May 23, 2025, 4:10am UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/9 "2025-05-23T04:10:53Z")

</div>

> [@carljm](#):
>
> If we were to agree on this interpretation, I think it would indicate a change in the current behavior of pyright and mypy: they should not interpret Callable-annotated attributes as bound-method descriptors

If I understand you correctly, you’re saying that because the `Callable` structural type doesn’t include a ` __get__ ` method in its definition, we should assume that no ` __get__ ` method is present on an object that conforms to this structural type. Is my understanding correct? If so, that doesn’t sound right to me. If we treat it like other structural types, then `Callable` says nothing about either the presence or absence of a ` __get__ ` method. That means we need to use other information and rules to determine the binding behaviors for an attribute that has a `Callable` type annotation, which is what mypy and pyright are doing.

I’m open to changing pyright’s behavior if it more accurately models the runtime behavior and we can get consensus from the community (including the mypy maintainers). However, we’d need to take into account the impact of such a change. I suspect there are many stubs, libraries, and source bases that rely on mypy’s and pyright’s current interpretation. Have you done any experiments to see what the fallout would be if we were to make a change here? My educated guess is that it would be a really painful change for users to swallow.

Here’s another idea. What if we added a few new special forms like `Method`, `ClassMethod` and `StaticMethod`? These would be subtypes of `Callable` but they would also include a ` __get__ ` method as appropriate. Like `Callable`, these would be structural types, as opposed to the the nominal types defined in `types.pyi` such as `MethodDescriptorType` and `ClassMethodDescriptorType`. They’d support the same generic parameterization as `Callable`. Perhaps that’s roughly what you had in mind when you said “splitting `Callable` into separate incompatible types”? If we were to do this, we could keep the current (mypy and pyright) behavior for `Callable` but implement the improved behavior for these new subtypes.

Another thing to consider is how any such change would affect typed decorators — those that use either TypeVars with a `Callable` upper bound or `Callable` with a `ParamSpec`. I mentioned above that pyright uses flags internally to track whether a function type is an regular function, an instance method, a class method, or a static method, and it uses these flags to inform the binding behavior. These flags are also captured and propagated by the constraint solver, so if a callable object is passed as an argument to a generic decorator function, the return result of the decorator retains these flags. I think mypy does something similar.

Code sample in [pyright playground](https://pyright-play.net/?pyrightVersion=1.1.399&code=GYJw9gtgBALgngBwJYDsDmUkQWEMoCCKcANFAMICGANtZQEbUCmAUGwCZPBScDGYAfXgImAN0ogA2gBUAXBRp1GTALoAKYPOkBKKAFoAfFDlQAdObYtedAM43CsllGdQAAn0HCxEpy87cIAEY1GyZqYDIAD3lUGF1DTBQYRxdUqAAiTIARMF4oGxgQVAxgXCggzPTfNKgQJhgAVxAUKAAGNlT3Jn4hRG8QardrSjsIeoALMHZB-3KAJjVrGyiYpPijWJSajOzc-MLiqFKQecrB1LrG5raOly6er3EBzoLKGCReMZhJ6dTZiAAzGpook4voNkktjVKjk8gUiugjmVAWdtrV6k0Wu02JQoABeQhqbRsAimIJqAhEsiBXRQADE5UCmHsBSQtCglBaqFeKF4THKEymLFJEAWNOcDNFzP2bOoHKgwzsAu%2BQpFQPF9PKAOlrPZuNe7zyXx%2BllmHgECAklAgNhEvEkACoHQAFMgAJXUmgUtAYzEkrqgHvW3qUfoDHvk5lMbEV9gAQlC3ObLSBrbbujMuIyQmEIlAQbFg5tzi4YXt4YdjozUdtLpiboN7oIU2m7Y3Y8ahX8s6LFtRlvnVmCEsW0WW4QdEVXRTWanXrtjOsmrTa2y8YG8Pp3fn4e0CC2twaDE6lx-sESVkQDZ2l51jLPR8VA40S2HGycEX9pqbTJUykCy7x6lyKA8nyyomu%2BvYapKcw6kBcq4rGEFClB6q-lq8GyvKBofCh0wsEAA)

In addition to these flags, pyright also captures the docstring of the pre-decorated function so when you hover over references to the post-decorated function, you’ll still see the docstring in your editor. Are you doing something similar in ty and pyrefly? If not, you may find that existing typed decorators don’t work in a compatible way.

---

<div class="post-metadata">

**Author:** ![xmw](https://avatars.discourse-cdn.com/v4/letter/x/aca169/32.png) [@xmw](https://discuss.python.org/u/xmw)\
**Post date:** [May 23, 2025, 8:58pm UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/10 "2025-05-23T20:58:51Z")

</div>

> [@carljm](#):
>
> ```python
> def impl(x: int) -> int:
> return x
> 
> class C:
> is_a_descriptor_at_runtime: ClassVar[Callable[[int], int]] = impl
> is_not_descriptor_at_runtime: ClassVar[Callable[[int], int]] = staticmethod(impl)
> 
> ```

This looks less to do with `Callable`s and more to do with descriptors in general. Here’s a non-`Callable` example:

```python
from typing import Any, ClassVar

class IntDescriptor(int):
    def __get__ (self, instance: Any, owner: type[Any], /) -> str:
        return str(self)

class A:
    val1: ClassVar[int] = 0
    val2: ClassVar[int] = IntDescriptor() # Type-checkers currently don't flag this
    val3: ClassVar[IntDescriptor] = IntDescriptor()

a: A = A()
reveal_type(a.val1) # int
reveal_type(a.val2) # int
reveal_type(a.val3) # str

```

How about making type-checkers flag such an assignment if either the instance or class-accessed type isn’t assignable to the given annotation? This is assuming that we’re still talking about the possibility of `Callable` of being method descriptors, but it should still apply to other descriptor cases regardless.

```python
def impl(x: int) -> int:
    return x

class C:
    is_a_descriptor_at_runtime: ClassVar[Callable[[int], int]] = impl
    # Error: Class variable expression of type `staticmethod[[int], int]` is incompatible with instance attribute of type `Callable[[], int]`
    # Hint: Use `ClassVar[staticmethod[[int], int]]`
    is_not_descriptor_at_runtime: ClassVar[Callable[[int], int]] = staticmethod(impl)

```

```python
class A:
    val1: ClassVar[int] = 0
    # Error: Class variable expression of type `IntDescriptor` is incompatible with class attribute of type `str`
    # Hint: Use `ClassVar[IntDescriptor]`
    # Error: Class variable expression of type `IntDescriptor` is incompatible with instance attribute of type `str`
    # Hint: Use `ClassVar[IntDescriptor]`
    val2: ClassVar[int] = IntDescriptor()
    val3: ClassVar[IntDescriptor] = IntDescriptor()

```

---

<div class="post-metadata">

**Author:** ![duncathan](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/duncathan/32/25521_2.png) [@duncathan](https://discuss.python.org/u/duncathan)\
**Post date:** [May 27, 2025, 10:02pm UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/11 "2025-05-27T22:02:51Z")

</div>

> [@erictraut](#):
>
> Here’s another idea. What if we added a few new special forms like `Method`, `ClassMethod` and `StaticMethod`? These would be subtypes of `Callable` but they would also include a ` __get__ ` method as appropriate. Like `Callable`, these would be structural types, as opposed to the the nominal types defined in `types.pyi` such as `MethodDescriptorType` and `ClassMethodDescriptorType`. They’d support the same generic parameterization as `Callable`. Perhaps that’s roughly what you had in mind when you said “splitting `Callable` into separate incompatible types”? If we were to do this, we could keep the current (mypy and pyright) behavior for `Callable` but implement the improved behavior for these new subtypes.

I’ve found myself wishing for precisely this before, in instances where my code handles bound methods in a different way than it handles other sorts of callable. I’ve had success rolling my own `Protocol` to describe a bound method more completely, but it’s such a fundamental element of the language that a dedicated special form would be preferable, IMO.

---

<div class="post-metadata">

**Author:** ![JukkaL](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jukkal/32/300_2.png) [@JukkaL](https://discuss.python.org/u/JukkaL)\
**Post date:** [May 28, 2025, 1:22pm UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/12 "2025-05-28T13:22:12Z")

</div>

Adding new flavors of `Callable` as suggested by @erictraut sounds more promising than tweaking than the semantics of `Callable`. We’ve tried to improve the behavior of `Callable` in mypy in the past, but it’s very easy to break some important existing use cases. I feel like we’ve reached this local maximum where it works for most (but not all) users, but it’s really to hard to make significant further progress while retaining sufficient backward compatibility.

I think that estimating the impact of proposed changes empirically would be good thing, however – maybe backward compatibility is worth breaking if the benefits are big enough.

---

<div class="post-metadata">

**Author:** ![rtpg](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rtpg/32/14068_2.png) [@rtpg](https://discuss.python.org/u/rtpg)\
**Post date:** [June 13, 2025, 3:01am UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/13 "2025-06-13T03:01:07Z")

</div>

Having a `MethodLike` protocol that includes both ` __call__ ` and the habitual ` __get__ ` semantics would be great for creating precise types for things like cache decorators (which often slap a couple extra attributes onto a function but are still returning a function with the binding behavior):

```python
class MethodLike[**P, RT](Protocol):
    def __call__ (*args: P.args, **kwargs: P.kwargs) -> RT: ...

    def __get__ [SelfType, **RestArgs](
        self: "MethodLike[Concatenate[SelfType, RestArgs], RT]",
        instance: SelfType,
        owner: object | None = None,
    ) -> "Callable[RestArgs, RT]": ...

```

^ I ended up doing something like this in one case earlier this week, and it’s been working well enough.

In the general case it’s not clear to me what the “proper” generic return type for ` __get__ ` would be here though, since the protocol shouldn’t prevent the `MethodLike` from implementing a custom ` __get__ ` that returns another `MethodLike` of a specific shape.

For cases where you _really_ are relying on function’s binding behavior, maybe even a protocol that is _purely_ focused on the binding behavior?

```python
class FunctionTypeInstanceBinding(Protocol):
    def __get__ [SelfType, **RestArgs](
        self: "Callable[Concatenate[SelfType, RestArgs], RT]",
        instance: SelfType,
        owner: object | None = None,
    ) -> "Callable[RestArgs, RT]": ...

```

* * *

Something that might be even more precise is being able to have `FunctionType` in `typing` with generics support so one could build their interfaces/protocols off of `FunctionType[P, RT]`. That feels precise and matches what people are doing in the “create a function and then slap some attributes onto it”.

I personally would love to have a lot more publicly accessible type sigs for these cases. I would love a type specificly for bound methods, for `FunctionType`s, etc so when writing library code I can be very pedantic.

(EDIT: after typing this up I found a whole slew of nits and things wrong with all of my code examples… someone with a better understanding than I of all of this should be in charge of determining the actual cutpoint. I do still think that if possible having a name for these protocols would be very good and I believe can be sound?)

---

<div class="post-metadata">

**Author:** ![NeilGirdhar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/neilgirdhar/32/5776_2.png) [@NeilGirdhar](https://discuss.python.org/u/NeilGirdhar)\
**Post date:** [June 15, 2025, 4:42pm UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/14 "2025-06-15T16:42:41Z")

</div>

One possible resolution to this issue might be to support [annotations of partial applications in general](https://github.com/python/typing/issues/1372). This could then be used to generally create all of the flavors of callables that Eric was talking about, which I illustrated [here](https://github.com/python/typing/issues/1372#issuecomment-1487702863).

I understand that Pyright currently handles this using flags—likely for performance reasons—which makes sense. That said, using flags can sometimes lead to edge cases where the behavior is hard to get exactly right. For example, the propagation of flags through decorators is not explicitly defined, invisible to the user, and can lead to type errors.

As type checkers continue to improve in speed, it could be worth revisiting solutions that handle these edge cases more robustly.

---

<div class="post-metadata">

**Author:** ![alwaysmpe](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alwaysmpe/32/23663_2.png) [@alwaysmpe](https://discuss.python.org/u/alwaysmpe)\
**Post date:** [June 21, 2025, 4:16am UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/15 "2025-06-21T04:16:13Z")

</div>

My question is should class member annotations relate to the type of value held in the class or the type returned by the access to that attribute? The obvious example is whether a read/write/delete property is compatible with the equivalent “pure” member variable annotation.

```python
class A:
    val: int

class B(A):
    @property
    def val(self) -> int: return 3
    @val.setter
    def val(self, value: int) -> None: return
    @val.deleter
    def val(self) -> None: return

```

mypy accepts this (and rejects the subclass if the property is not writeable), pyright rejects the subclass as `property` is not considered as compatible with `int`. I prefer the mypy behaviour here. The [descriptor guide](https://docs.python.org/3/howto/descriptor.html#customized-names:~:text=The%20property()%20builtin%20helps%20whenever%20a%20user%20interface%20has%20granted%20attribute%20access%20and%20then%20subsequent%20changes%20require%20the%20intervention%20of%20a%20method) also highlights that a property can be used to allow interface changes without breaking compatibility.

From a user’s perspective I don’t think having a get/set/delete property in a type stub would have any benefit over the equivalent pure variable.

Following that logic the case when different types are returned when accessed via a class vs instance could be modelled with a `Protocol` that implements the descriptor protocol to differentiate between access.

```python
from typing import Protocol
class DualVar[ClassGetT, MemberGetT](Protocol):
    @overload
    def __get__ (self, instance: None, owner: type, /) -> ClassGetT: ...
    @overload
    def __get__ (self, instance: Any, owner: type | None = None, /) -> MemberGetT: ...

```

This is similar to the suggestion to add new special forms for `Method`, etc but slightly more generic and compatible with custom descriptors. Type checkers then would verify whether values assigned satisfy the implied behaviour. Users only need to know about the behaviour of the value returned on access.

Using this then the below would all be acceptable and all annotate the same instance access behaviour.

```python
from typing import ClassVar, Callable

class C:
    c_fn: ClassVar[DualVar[Callable[[C, int], int], Callable[[int], int]]]

class D:
    def d_fn(self, arg: int) -> int:
        return arg

def e_fn(_, arg: int) -> int:
    return arg

class E:
    e_fn: Callable[[int], int] = e_fn

```

Currently mypy and pyright both reject the last one. However, at runtime when accessed via an instance you get the annotated callable type and would have the same behaviour if you set a callable of that signature on the instance. `dataclass` would break this of course as it doesn’t set defaults in the class dictionary I think.

---

<div class="post-metadata">

**Author:** ![alwaysmpe](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alwaysmpe/32/23663_2.png) [@alwaysmpe](https://discuss.python.org/u/alwaysmpe)\
**Post date:** [June 21, 2025, 2:37pm UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/16 "2025-06-21T14:37:53Z")

</div>

From an implementation point of view, I think both mypy and pyright model descriptors very well. Currently there are some flags used to implement the likes of `property` and `classmethod`, but I believe both type checkers will correctly model the equivalent manually created descriptors. I played around with removing the special casing for mypy and relying on just the descriptor protocol and using appropriate type annotations for `classmethod`, `property`, etc and got most of the way there at one point, I think `property.setter` etc required redefinitions and were giving me some trouble. But then life got busy.

I’m somewhat averse to add new special forms like `Method` as type checkers can already correctly model the behaviour.

As was pointed out, if `Callable` doesn’t currently require ` __get__ ` then if a `Callable` was placed in the class dictionary then this wouldn’t satisfy the typing behaviour specified by a `DualVar` type that applies `self` binding. Instead the type would need to be constrained to something that implements the binding behaviour. Or accept something typed as a generic `Callable` as having unspecified binding behavior and require that it be wrapped in a clearly specified descriptor.

Then I think the biggest potential for unsoundness is a callable parent class with a child that implements ` __get__ `. something that type checkers could warn against and advise composition be used instead. Maybe add a decorator that performs `self` binding that is a no-op for standard `functions` but adds binding behaviour to custom callable classes.

---

<div class="post-metadata">

**Author:** ![jorenham](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jorenham/32/27806_2.png) [@jorenham](https://discuss.python.org/u/jorenham)\
**Post date:** [June 21, 2025, 5:06pm UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/17 "2025-06-21T17:06:13Z")

</div>

Note that descriptors can be unsafe, which was pointed out in [report unsafe ` __get__ ` return types · Issue #629 · astral-sh/ty · GitHub](https://github.com/astral-sh/ty/issues/629) by @kotlinisland:

```py
class A: 
    pass

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

class C:
    a: A = B()

C.a # static: A, runtime: 1

```

So that’s something to keep in mind here

---

<div class="post-metadata">

**Author:** ![alwaysmpe](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alwaysmpe/32/23663_2.png) [@alwaysmpe](https://discuss.python.org/u/alwaysmpe)\
**Post date:** [June 21, 2025, 5:33pm UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/18 "2025-06-21T17:33:11Z")

</div>

Good catch thank you. I’d seen the concern for `Callable` but didn’t realize this was a general problem for all objects in the class dictionary. Your link is a little broken I think you meant [this](https://github.com/astral-sh/ty/issues/629).

---

<div class="post-metadata">

**Author:** ![jorenham](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jorenham/32/27806_2.png) [@jorenham](https://discuss.python.org/u/jorenham)\
**Post date:** [June 21, 2025, 11:08pm UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/19 "2025-06-21T23:08:35Z")

</div>

> [@alwaysmpe](#):
>
> Your link is a little broken I think you meant [this](https://github.com/astral-sh/ty/issues/629)

Oh yea woops, fixed it

---

<div class="post-metadata">

**Author:** ![stroxler](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stroxler/32/9681_2.png) [@stroxler](https://discuss.python.org/u/stroxler)\
**Post date:** [July 10, 2025, 6:12pm UTC](https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938/20 "2025-07-10T18:12:47Z")

</div>

I wanted to clarify a point I originally misunderstood reading this thread while looking into how Pyrefly handles the ambiguous cases.

At one point we said:

> The typing spec is pretty clear that the `Callable` special form describes a structural type that has a ` __call__ ` method. In other words, it’s comparable to a protocol class with a ` __call__ ` method. No other methods (such as ` __get__ ` ) are implied.

This is true of the spec, but I would actually conclude that Pyright and MyPy both actually assume a value of type `Callable` does implement ` __get__ `, as discussed in the later followup beginning with

> If I understand you correctly, you’re saying that because the `Callable` structural type doesn’t include a ` __get__ ` method in its definition, we should assume that no ` __get__ ` method is present on an object that conforms to this structural type.

and confirmed by [this example](https://pyright-play.net/?pyrightVersion=1.1.374&pythonVersion=3.12&analyzeUnannotatedFunctions=true&code=GYJw9gtgBALgngBwJYDsDmUkQWEMoDCAhgDYlEBGJApgDRQAK4MYAxmCfQeQM48BqREAChRrXjygBZOMTIUirANZMwLdiQDaAKm0N6AFQC6AClXqOASgBcwqPagATasCgB9N61IkPJntRJgem0hNB5rRgA6UJ5g7SUAdxiIhkjEmMsoAFoAPigDWwciqEjS0WdXZ3YQIhYQAAZNAziGU2AIuXIqak19fKNM3MJvShpewyNChxBqGABXEBQoYHKXJ2pq2twARiaWto6R7vH%2BwbyZToVlczYOE%2BMp%2Bxn5xeXRAGIoAEF8EDmUGBYOhQChqAAWUAABsAwGBIVAiChHFCFCB4UJqFAILMwWBHJITIjkQA3aggOBQEhIJQBCkwMGY9jOBFzem4TAA6hIyT0pA8SyRYSfL6saqOVAYFiwBmwRASqA8BAbegoahIelkqBgVWYSQoNQIrE4vEgjZEOb%2BKCq9UMkBQMFESREWVKzDYGjYgHyjxoWYeYTiR2SAiPKAAASquC2DTsDgqy1h9T8AWAZygADltdQIggg6IihGNlG6ttY-Z4zCwNtk4E05nVTm88ICCYBZWk5koJ8-l7sRFngsUJJ68D4Mh0AqlawFUQ4DywXzdYax5iyeARC224mTJvO93-oC%2B8siEgSLEXfLFRsZ3PpYvFxqtSgSBTiaQkMiknSwFAvGQoZukSVvCoiAZW1Z7lAPaHtmUGzIOw5ZvQY6XlON7zveTouquIDrs2rZAbC1a7vY%2B69rBwAnmeyFyhOV7Tjws4YZID4ytqL5QG%2BVKfrOsA-n%2BJAAQRwFQKIwjxhARA0p4Rw0K22R5J0ow9JoYAUAAVhsMD0KgMBGDpAKTCUZQBhIUAACKhu0wxkMpmiqRpWkGXpzlGFAAC8WJSdQMm2d0rZllAaAdBIgggJoSnHA5mmsNpHIufFRhuZ5knSQJykBYF8YeKg6q%2BP4tahkUBXAEBHmUkQEAUI4zoAB70HAERwKI5nCSYakxTArb0AArJYwjmUB7WObF3VQH1A0EWgJgTcIQA) (mypy gives the same answers).

The reason I conclude this is that for both decorators and assignment to `ClassVar`, Pyright and Mypy are indeed applying the descriptor behavior; the instance shadowing semantics are a related but distinct question from whether to assume ` __get__ ` exists in cases where we know method binding _would_ occur _if_ the value has a ` __get__ `.

I’m in alignment with this behavior, I think assuming `Callable` does have ` __get__ ` is almost certainly a good choice, since in practice most actual values described as `Callable` are either function defs or lambdas.

* * *

My current concern is slightly different, it’s how to treat callback protocols, which are very similar to `Callable` but have divergent behavior in Pyright and mypy.

Pyright and mypy both assume that a callback protocol, unlike `Callable`, implies that ` __get__ ` is _not_ present; the same [example](https://pyright-play.net/?pyrightVersion=1.1.374&pythonVersion=3.12&analyzeUnannotatedFunctions=true&code=GYJw9gtgBALgngBwJYDsDmUkQWEMoDCAhgDYlEBGJApgDRQAK4MYAxmCfQeQM48BqREAChRrXjygBZOMTIUirANZMwLdiQDaAKm0N6AFQC6AClXqOASgBcwqPagATasCgB9N61IkPJntRJgem0hNB5rRgA6UJ5g7SUAdxiIhkjEmMsoAFoAPigDWwciqEjS0WdXZ3YQIhYQAAZNAziGU2AIuXIqak19fKNM3MJvShpewyNChxBqGABXEBQoYHKXJ2pq2twARiaWto6R7vH%2BwbyZToVlczYOE%2BMp%2Bxn5xeXRAGIoAEF8EDmUGBYOhQChqAAWUAABsAwGBIVAiChHFCFCB4UJqFAILMwWBHJITIjkQA3aggOBQEhIJQBCkwMGY9jOBFzem4TAA6hIyT0pA8SyRYSfL6saqOVAYFiwBmwRASqA8BAbegoahIelkqBgVWYSQoNQIrE4vEgjZEOb%2BKCq9UMkBQMFESREWVKzDYGjYgHyjxoWYeYTiR2SAiPKAAASquC2DTsDgqy1h9T8AWAZygADltdQIggg6IihGNlG6ttY-Z4zCwNtk4E05nVTm88ICCYBZWk5koJ8-l7sRFngsUJJ68D4Mh0AqlawFUQ4DywXzdYax5iyeARC224mTJvO93-oC%2B8siEgSLEXfLFRsZ3PpYvFxqtSgSBTiaQkMiknSwFAvGQoZukSVvCoiAZW1Z7lAPaHtmUGzIOw5ZvQY6XlON7zveTouquIDrs2rZAbC1a7vY%2B69rBwAnmeyFyhOV7Tjws4YZID4ytqL5QG%2BVKfrOsA-n%2BJAAQRwFQKIwjxhARA0p4Rw0K22R5J0ow9JoYAUAAVhsMD0KgMBGDpAKTCUZQBhIUAACKhu0wxkMpmiqRpWkGXpzlGFAAC8WJSdQMm2d0rZllAaAdBIgggJoSnHA5mmsNpHIufFRhuZ5knSQJykBYF8YeKg6q%2BP4tahkUBXAEBHmUkQEAUI4zoAB70HAERwKI5nCSYakxTArb0AArJYwjmUB7WObF3VQH1A0EWgJgTcIQA) includes a decorator using a callback protocol and there both type checkers treat the decorated method as not a method.

This strikes me as problematic both conceptually and practically:

- Conceptually, it’s weird because `Callable` and the callback protocol are supposed to be equivalent but they aren’t; this seems confusing for decorator library authors.
- Practically, I think the issue is that “most of the values” that are typed as callback protocols are probably actually function defs, just as with `Callable`.

I observed [a case where Pytorch ran into this problem](https://github.com/pytorch/pytorch/blob/e3fe001d9e38153a7797ad25377fdfa34e461113/torch/_inductor/utils.py#L625), and they solved it with an approximation where the decorator directly strips the first argument. This is workable because it gets instance access right, but is problematic for multiple reasons:

- class access will be type-analyzed incorrectly; at runtime it take a `self` argument, but the approximated callback protocol says it has no `self`
- the actual type of `self` has been stripped, which is okay here but would mess up constraint solving in a more complex example

But I’m not sure what to do about it in Pyrefly; I feel like treating callback protocols as implying no ` __get__ ` is probably wrong for most uses but:

- Compatibility is important, so I hesitate to diverge from mypy and Pyright
- There’s an argument that this behavior is needed so that there’s a way to express a custom, non-descriptor callable type as a protocol, so some edge cases become hard to express
