# Draft typing spec chapter for constructors

**URL:** <https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744>\
**Category:** Typing\
**Tags:** typing-spec\
**Created:** [March 28, 2024, 2:26pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744 "2024-03-28T14:26:56Z")\
**Posts on this page:** 20\
**Page:** 1

<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:** [March 28, 2024, 2:26pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/1 "2024-03-28T14:26:56Z")

</div>

As part of the continuing effort to fill in missing chapters in the typing spec, I’ve written a [draft chapter for constructors](https://github.com/python/typing/pull/1667).

This is an area where we see significant divergence in behavior between type checkers, so it would be good to agree on a standard set of behaviors.

For small typos or wording improvement suggestions, feel free to comment directly in the PR. For larger discussion topics, let’s use this thread for visibility.

---

<div class="post-metadata">

**Author:** ![Jelle](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jelle/32/1049_2.png) [@Jelle](https://discuss.python.org/u/Jelle)\
**Post date:** [March 28, 2024, 3:10pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/2 "2024-03-28T15:10:17Z")

</div>

Thank you!

> [@erictraut](#):
>
> This is an area where we see significant divergence in behavior between type checkers, so it would be good to agree on a standard set of behaviors.

Did you already write a set of conformance tests for this chapter? It would be good to see the results so we know which aspects diverge from current type checker behavior and therefore need a closer look.

One thing I was surprised by: Your draft states that if ` __new__ ` returns Any, then the return type should be ignored and the method should be assumed to return an instance of the class. That seems wrong to me; if the user explicitly said Any, shouldn’t we believe them? I checked ([pyright](https://pyright-play.net/?pyrightVersion=1.1.353&pythonVersion=3.13&strict=true&enableExperimentalFeatures=true&code=GYJw9gtgBAzpCmB3AFvE8oEsIAcwgBcoBlBAYQBsBDGGAKFEigIE8dMA7Acy13yOLwKwBuGit23XnkJQAghxZ06AY2q15ALjpRdUACbxgUAPomOSMwAo1MAJRQAtAD4SQ4Nr1eoIKphgYAHJgBACSuBTwEPAcBPD6AKIg4CDKajQwUABCnnqGxmYWiNa2Di7yirnePn4BUMFhEVExcYnJ%2BDp6quqZZFUGRqbmliY2FPZOrqTRlBn9Xr7%2BQSHhOJHRsfFJKZ263RlQACL9%2BUNFJeN283qLdQ2r6y1b7an7Ggkng4UjYxPlnARrrpbstGmtmps2js6OgAG7wKgUEwSeBWORWOx2GHweGI5FsVFZDFYuEIpEoqxkYnY3HkglWQ7U0l4ikJYlAA), [mypy](https://mypy-play.net/?mypy=latest&python=3.12&gist=70aeb63712d167337c36e50e1b357235)) and both use this behavior even when the return type is an implicit Any (from an unimportable module). However, mypy generally isn’t great at believing ` __new__ ` return types and this is something we’re already actively considering changing.

The “Converting a Constructor to Callable” section says that to infer the callable type, type checkers should look at the ` __init__ ` method first, then ` __new__ `. But isn’t that backwards? At runtime, ` __call__ ` is used first, then ` __new__ `, then ` __init__ `. If the metaclass’s ` __call__ ` has a very different signature, then the callable type of the class object should reflect that. Here’s a [test case](https://pyright-play.net/?pyrightVersion=1.1.353&pythonVersion=3.13&strict=true&enableExperimentalFeatures=true&code=GYJw9gtgBALgngBwJYDsDmUkQWEMoDCAhgDYlEBGJApgFC0DG5Azs1ALIAU8C1AlAC5aUEVAAm1YFAD60hqRKzOzaiWAAaKEQGYUMPlAC0APl0whoy1BDUYAVxAooAZmFWRjFmwCCnCLaImIlYAXnZBNxEJKVlUJBglFTVNCh1mGBADEygAOTAUagt3KARg5npo2CIAa2ppVAT5MkoaTgYdYmaqagBtHoaAXU1BwSgAOgn6GBq6hrkFFupObz5aGwA3alJpHiXfAEY%2BVaA) where pyright behaves oddly due to this rule: `A(1)` is inferred as being of type `int`, but `A` is not compatible with `Callable[[int], int]`.

---

<div class="post-metadata">

**Author:** ![tmk](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tmk/32/10935_2.png) [@tmk](https://discuss.python.org/u/tmk)\
**Post date:** [March 28, 2024, 3:46pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/3 "2024-03-28T15:46:13Z")

</div>

Re: “Constructor Calls for `type[T]`”, I’ve come to realize that when people use `type[T]`, what they usually actually want is `Callable[[...], T]` (where ideally the argument types are specified explicitly). The latter is actually type-safe (no subclassing problem), more flexible (you can also pass in functions in addition to classes), and generally more robust and explicit.

The only situation I can think of where you genuinely need `type[T]` is when you want to call static or class methods on the class object. But you’d probably be better off with defining a `Protocol` for that case:

```python
class C:
  @staticmethod
  def f(x: int) -> str: ...

class P(Protocol):
  def f(self, x: int) -> str: ...

def g(cls: P):
  print(cls.f(0))

g(C) # passes in mypy and pyright

```

So, I wonder whether it shouldn’t just be left to type checkers whether they want to support constructors on `type[T]` or not?

---

<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:** [March 28, 2024, 3:46pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/4 "2024-03-28T15:46:47Z")

</div>

> [@Jelle](#):
>
> Did you already write a set of conformance tests for this chapter?

I haven’t done this yet. I was hoping to get some feedback on the draft before investing the time in a full conformance test suite. Rather than writing a full test suite at this time, I can write a subset to get a sense for current type checker behaviors. I’ll post back here when I have those results.

> [@Jelle](#):
>
> One thing I was surprised by: Your draft states that if ` __new__ ` returns `Any`, then the return type should be ignored

I’m not very convicted on this point. However, I think it’s important that if the ` __new__ ` method is unannotated and its return type is inferred as `Any` (or `Unknown`, using pyright’s terminology), then a type checker should assume a return type of `Self`. If a ` __new__ ` is explicitly annotated to return `Any`, then I think there’s a reasonable argument that the constructor call should evaluate to `Any`. I’m not sure what that means for the ` __init__ ` evaluation though. Should a type checker assume that the ` __init__ ` method is _not_ called in this case? Or that it _is_ called? The former will mask errors (false negatives), and the latter could potentially lead to false positives.

> [@Jelle](#):
>
> type checkers should look at the ` __init__ ` method first

The reason that pyright looks at ` __init__ ` methods first when converting to a callable is that ` __init__ ` method is typically richer in type information. ` __new__ ` method signatures often consist of `(cls, *args, **kwargs)` which is not very useful from a typing perspective. It appears that mypy does the same — presumably for the same reason.

---

<div class="post-metadata">

**Author:** ![mikeshardmind](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mikeshardmind/32/14381_2.png) [@mikeshardmind](https://discuss.python.org/u/mikeshardmind)\
**Post date:** [March 28, 2024, 4:26pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/5 "2024-03-28T16:26:49Z")

</div>

Perspective on why some of the current behavior and user types are the way they are might help here.  
Please don’t read the below as “we need to enforce LSP on ` __init__ ` / ` __new__ `”, that’s _a_ way forward, but it’s not the only one, and as prior discussions got into, there’s reasons why this wasn’t done.

> [@tmk](#):
>
> Re: “Constructor Calls for `type[T]`”, I’ve come to realize that when people use `type[T]`, what they usually actually want is `Callable[[...], T]` (where ideally the argument types are specified explicitly). The latter is actually type-safe (no subclassing problem), more flexible (you can also pass in functions in addition to classes), and generally more robust and explicit.
> 
> The only situation I can think of where you genuinely need `type[T]` is when you want to call static or class methods on the class object. But you’d probably be better off with defining a `Protocol` for that case:

1. This is only the case due to type checkers not enforcing LSP for ` __init__ ` and might indicate that prior proposals to copy a signature for sure elsewhere paired with intersections would help here (ie. `T & Callable[T. __init__ , T]`) This would ensure that a type could be constructed as expected.

2. While it might be better to define a complex protocol for some of those cases, I don’t see many people being willing to do it when `type[T]` appears to work until someone makes a subclass that violates LSP on ` __init__ `

> [@erictraut](#):
>
> > [@Jelle](#):
> >
> > One thing I was surprised by: Your draft states that if ` __new__ ` returns `Any`, then the return type should be ignored
> 
> I’m not very convicted on this point. However, I think it’s important that if the ` __new__ ` method is unannotated and its return type is inferred as `Any` (or `Unknown`, using pyright’s terminology), then a type checker should assume a return type of `Self`. If a ` __new__ ` is explicitly annotated to return `Any`, then I think there’s a reasonable argument that the constructor call should evaluate to `Any`. I’m not sure what that means for the ` __init__ ` evaluation though. Should a type checker assume that the ` __init__ ` method is _not_ called in this case? Or that it _is_ called? The former will mask errors (false negatives), and the latter could potentially lead to false positives.

I don’t think it’s a good idea to have a situation where Annotating with the inferred type without an annotation (Any) changes the behavior. This feels like it also is partially necessary due to how ` __init__ ` and ` __new__ ` are not required to be LSP compatible.

There might be a better inferrable type than Any using the method that was mentioned [in the discussion of subtyping involving Any](https://discuss.python.org/t/take-2-rules-for-subclassing-any/47981/7), but this isn’t something that would be user-denotable currently or for a while.

> [@erictraut](#):
>
> The reason that pyright looks at ` __init__ ` methods first when converting to a callable is that ` __init__ ` method is typically richer in type information. ` __new__ ` method signatures often consist of `(cls, *args, **kwargs)` which is not very useful from a typing perspective. It appears that mypy does the same — presumably for the same reason.

The ability to partially copy signatures might lead to more people typing ` __new__ `accurately, speaking for only myself, the inability to do so is the primary reason I don’t give new more detailed typing. The secondary reason is that type checkers largely ignore it anyway.

---

<div class="post-metadata">

**Author:** ![Jelle](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jelle/32/1049_2.png) [@Jelle](https://discuss.python.org/u/Jelle)\
**Post date:** [March 28, 2024, 4:48pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/6 "2024-03-28T16:48:00Z")

</div>

Regarding the `Callable` signature, I feel the most principled solution may be that the callable type of the class should be (informally) the union of the ` __call__ `, ` __new__ `, and ` __init__ ` signatures.

For instance, if class `A` has a metaclass ` __call__ ` `(*args, **kwargs) -> Self`, ` __new__ ` `(*args,**kwargs) -> Self`, and ` __init__ ` of `(a: int) -> None`, then this should “sum” to `(a: int) -> Self`. But if ` __new__ ` is `(a: int) -> Self` and ` __init__ ` is `(*args: int) -> None`, then this should sum to `(a: int) -> None`.

---

<div class="post-metadata">

**Author:** ![Daverball](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/daverball/32/15931_2.png) [@Daverball](https://discuss.python.org/u/Daverball)\
**Post date:** [March 28, 2024, 4:51pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/7 "2024-03-28T16:51:31Z")

</div>

I’m looking forward to this being properly specified, since this is one of the areas where I’m jealous of pyright users, because it much better reflects the dynamic object model of Python. That being said I think there is still one case that could use improvement, and that is when ` __new__ ` is overloaded with different return types.

I think pyright’s heuristic is of preferring ` __init__ ` over ` __new__ ` when converting to `Callable` works well in all the cases, except for this one. I think it would be better to retain the signature of ` __new__ ` in this case for all the overloads, except for the ones returning `Self`. But Jelle’s approach of creating a union type might work as well.

I have here a little toy example inspired by WTForm’s API\[1\], that I think illustrates where the current rule can be inadequate:

Code sample in [pyright playground](https://pyright-play.net/?pythonVersion=3.12&strict=true&enableExperimentalFeatures=true&code=GYJw9gtgBAxmA28CmMAuBLMA7AzgOgEMAjGKdCABzBFSgGEDFjkBYAKFEilQE8L0sAczKVqtMADckIeGAIATADRQAglh7KAyknjB27GPAI4cUAGLUIALih47%2BtoeOmAqliJgArlnln0O%2BQBtABUAXSt2KCioYH94eQB9QxwbXgokENCHJxNzOPkItmioAAFJaVkFSOj5JGAoBISsJAB3RoAKZOUAKgIQQRTVdWUE4EsbADlsJCgAXigp5p7ugGsWvoGbNR4ASigAWgA%2BKDcPb198wO1dcNt7IuiyqRk5eWqo2vrG5raEzvgcD0NoNtiMxiBrOZLMs1sCtuo9kcoNdgDY7Hh3lBPg0mq0Ol0oL1%2BiDhoTVutifDdgdjiioAAfE7uLw%2BPwBK46YC3dGY7GNAToVAdHCc5QYVDIGw4VAgOZQADk8p6YPGUIhiOOiyQaPuBiMuU0niIsQC7TZ8R2hWKfISAqFfxFujFgslUGlsvmiuUSAAHjKCAkNjYBLRGVq5VrlQ1wZCLOqaQtpjqMWwHNjUGAkowjERkCFlgAFUL-QYMJi5jIF5RhDX0bPMSvV7m61NsEBIKSMBJpJDtDNZ8vIM35HY7djtzvwbt8Xv9mD1ivtQ3GkdjtsdpBdnvD02j8cbrczpdGk3xdp79eT6fpHdn0aWWZxiDni8TzdT7fL0-ydr3iGPywXzXIA)

```python
from collections.abc import Callable
from typing import overload, Any, Self

class Form: ...

class UnboundField[T]:
    field_cls: type[T]

class Field:
    @overload
    def __new__ (cls, *args: Any, _form: None = None, **kwargs: Any) -> UnboundField[Self]: ...
    @overload
    def __new__ (cls, *args: Any, _form: Form, **kwargs: Any) -> Self: ...
    def __new__ (cls, *args: Any, **kwargs: Any) -> Self | UnboundField[Self]: ...
    def __init__ (self, title: str = '', *, _form: Form) -> None: ...

class Subfield(Field):
    def __init__ (self, title: str = '', extra_arg: int | None = None, *, _form: Form) -> None: ...

def to_callable[T, **P](cls: Callable[P, T]) -> Callable[P, T]: ...

reveal_type(to_callable(Field))
reveal_type(to_callable(Subfield))
reveal_type(Field())
reveal_type(Subfield())
reveal_type(Field(_form=Form()))
reveal_type(Subfield(_form=Form()))

```

* * *

1. I make no statement about the quality of the API, it just so happens to be one I contributed stubs to typeshed for, so I know it’s a real use-case

---

<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:** [March 28, 2024, 4:56pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/8 "2024-03-28T16:56:27Z")

</div>

Here are some quick-and-dirty conformance test results indicating where each type checker is consistent with the latest draft specification and where it’s not.

| Area | mypy | pyright | pyre | pytype |
| --- | --- | --- | --- | --- |
| Honors metaclass ` __call__ ` | no | yes | yes | no |
| ` __new__ ` basic specialization | yes | yes | yes | partial |
| Honors ` __new__ ` return type | no | yes | no | no |
| Skips ` __init__ ` based on ` __new__ ` return type | no | yes | no | no |
| Validates binding of ` __new__ ` cls | no | partial | no | no |
| ` __init__ ` basic specialization | yes | yes | yes | partial |
| Validates binding of ` __init__ ` self | no | partial | no | no |
| Honors ` __init__ ` self annotation (basic) | partial | yes | yes | no |
| Honors ` __init__ ` self annotation (func TypeVar) | no | no | yes | no |
| Rejects ` __init__ ` self annotation (class TypeVar) | no | no | no | no |
| Constructor specialization defaults | partial | yes | no | no |
| Converting constructor to callable | yes | yes | yes | partial |

---

<div class="post-metadata">

**Author:** ![mikeshardmind](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mikeshardmind/32/14381_2.png) [@mikeshardmind](https://discuss.python.org/u/mikeshardmind)\
**Post date:** [March 28, 2024, 5:42pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/9 "2024-03-28T17:42:44Z")

</div>

I’m also noticing that the behavior of ` __init_subclass__ ` is not covered in this section right now, is that intentional to be covered later or elsewhere?

---

<div class="post-metadata">

**Author:** ![Viicos](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/viicos/32/14616_2.png) [@Viicos](https://discuss.python.org/u/Viicos)\
**Post date:** [March 28, 2024, 5:43pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/10 "2024-03-28T17:43:51Z")

</div>

Thanks for picking this up! Regarding [Spec: Annotating the `self` argument in ` __init__ ` methods · Issue #1563 · python/typing · GitHub](https://github.com/python/typing/issues/1563) I think it makes more sense to tackle this as you did: first specify how constructors should be evaluated by type checkers, and add information related to generics in there (as you did).

After a quick reading, one question pops to mind. [This section](https://github.com/python/typing/blob/983d6e32b096a515ad3e426c9e9ac648f9d5dd6b/docs/spec/constructors.rst?plain=1#L74-L88) states the following:

> If any class-scoped type variables are not solved when evaluating the ` __new__ `  
> method call using the supplied arguments, these type variables should be left  
> unsolved, allowing the ` __init__ ` method to be used to solve them.

What if, in the example given, ` __new__ ` is defined as ` __new__ (cls) -> Self`?

I guess it’s up to the user to have compatible signatures between ` __new__ ` and ` __init__ `? (this only applies when ` __new__ ` doesn’t return `Any` or something else than `Self`, according to this spec addition).

---

<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:** [March 28, 2024, 6:08pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/11 "2024-03-28T18:08:34Z")

</div>

> [@mikeshardmind](#):
>
> ’m also noticing that the behavior of ` __init_subclass__ ` is not covered in this section right now, is that intentional to be covered later or elsewhere?

I was planning to include ` __init_subclass__ ` in a different chapter on metaclasses. It’s not related to the evaluation of constructor calls, so I don’t think it belongs in this chapter. In any case, this draft is already pretty long, so I don’t want to add more at this point.

---

<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:** [March 28, 2024, 8:46pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/12 "2024-03-28T20:46:06Z")

</div>

@Jelle, I like your suggestion about combining the ` __call__ `, ` __new__ ` and ` __init__ ` signatures into a union when converting a constructor to a callable, but I was concerned about the compatibility implications.

As an experiment, I quickly implemented your suggestion. To my surprise, it generated [no mypy\_primer changes](https://github.com/microsoft/pyright/pull/7585)!

Based on this, I’m going to rewrite that section of the draft spec to incorporate your proposal.

---

<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:** [April 3, 2024, 4:46pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/13 "2024-04-03T16:46:32Z")

</div>

Thanks to everyone who has reviewed and provided feedback on this chapter.

I’ve incorporated the feedback in the latest draft. I’ll leave it open for review and comments for another 24 hours. If I don’t receive any additional feedback, I plan to submit it to the TC for consideration.

---

<div class="post-metadata">

**Author:** ![Jelle](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jelle/32/1049_2.png) [@Jelle](https://discuss.python.org/u/Jelle)\
**Post date:** [April 3, 2024, 7:00pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/14 "2024-04-03T19:00:21Z")

</div>

Thanks. One area where I’m not convinced yet is the behavior when ` __new__ ` returns Any, as I mentioned before. I see the justification for the behavior, but it’s a special case and I’m not convinced user-defined ` __new__ ` methods are common enough to justify the special case. I’d be interested in hearing more opinions from the community on this point:

If the ` __new__ ` method of a class `C` is unannotated, or annotated as returning Any, should type checkers treat the return type as `Self`, so that constructing `C()` is inferred as returning `C`, or should type checkers assume that the return type is not known and infer `C()` as returning `Any`?

---

<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:** [April 3, 2024, 7:33pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/15 "2024-04-03T19:33:21Z")

</div>

> [@Jelle](#):
>
> One area where I’m not convinced yet is the behavior when ` __new__ ` returns Any

Yeah, I thought more about your feedback and adjusted the spec accordingly. Please review the latest draft and let me know what you think. I left you a comment in the PR to point out the updated section.

---

<div class="post-metadata">

**Author:** ![Jelle](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jelle/32/1049_2.png) [@Jelle](https://discuss.python.org/u/Jelle)\
**Post date:** [April 3, 2024, 7:44pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/16 "2024-04-03T19:44:33Z")

</div>

Oh great, thanks! I realized I was actually talking about ` __call__ `, not ` __new__ `, where the proposed spec still special-cases Any:

```python
If the evaluated return type of the `` __call__ `` method is something other than
``Any`` or an instance of the class being constructed, a type checker should
assume that the metaclass `` __call__ `` method is overriding ``type. __call__ ``
in some special manner, and it should not attempt to evaluate the `` __new__ ``
or `` __init__ `` methods on the class.

```

Sorry for mentioning the wrong method above. I think if we’re considering ` __call__ `, what I said above applies even more: overriding ` __call__ ` on a metaclass is not a common enough occurrence to warrant such a special case. Therefore, I would suggest to remove the “`Any` or” phrase in the paragraph I quoted above.

Given this test:

```python
from typing import Any, TypeVar

class Meta1(type):
    def __call__ (self, *args: Any, **kwargs: Any) -> Any:
        return super(). __call__ (*args, **kwargs)

class CallAny(metaclass=Meta1):
    pass

class Meta2(type):
    def __call__ (self, *args: Any, **kwargs: Any) -> int:
        return 42

class CallInt(metaclass=Meta2):
    pass

T = TypeVar("T")

class Meta3(type):
    def __call__ (self: type[T], *args: Any, **kwargs: Any) -> T:
        return super(). __call__ (*args, **kwargs)

class CallSelf(metaclass=Meta3):
    pass

reveal_type(CallAny())
reveal_type(CallInt())
reveal_type(CallSelf())

```

Current results:

- [mypy](https://mypy-play.net/?mypy=latest&python=3.12&gist=464ca85b1e6da10d6c0ad42fea70d2f7): CallAny, CallInt, CallSelf
- [pyright](https://pyright-play.net/?pythonVersion=3.13&strict=true&code=GYJw9gtgBALgngBwJYDsDmUkQWEMoCCKcANFACqICmAagIYgBQjAxgDZ0DOnUAslTDoBGABTwEVAJQAuRlHlQAJlWBQA%2BmpZ02bDSM5U2wMgCoGaTtMLFTJgNYB3c5etxJUALQA%2BV7IX%2BoEAEAVxAUKE5giRARSQA6DS0dPTMQC1tHZ0lmdi4eAGFtNiI4EQgBOlzuAF5%2BQSEZOQUEPJyObj4KgCYxakb-ZVVEor0DI1NnKxKMpzSXEvdvTBQYPwD5IJhQ8IAWLra8qEKdAEkVsoqqzlru-ubW5nIoaopqehiAInIP7NZ2njqdAAzL0JHd5IN1JoRmp9IZgFZxFQANrkAC6EzmUxsUBM9lmFmxbk8PnIa3Wm22ESiVBi8WGyVhqXSuPxWQOHWObAAyvCLoIrjdBEDwVAWtxmEEAG5UbRqJEiLklWLZaWy3QKrlnGAqxhquWaoq8oy6oA): CallAny, int, CallSelf
- [pyre](https://pyre-check.org/play?input=from%20typing%20import%20Any%2C%20TypeVar%0A%0Aclass%20Meta1(type)%3A%0A%20%20%20%20def%20 __call__ (self%2C%20*args%3A%20Any%2C%20**kwargs%3A%20Any)%20-%3E%20Any%3A%0A%20%20%20%20%20%20%20%20return%20super(). __call__ (*args%2C%20**kwargs)%0A%0Aclass%20CallAny(metaclass%3DMeta1)%3A%0A%20%20%20%20pass%0A%0Aclass%20Meta2(type)%3A%0A%20%20%20%20def%20 __call__ (self%2C%20*args%3A%20Any%2C%20**kwargs%3A%20Any)%20-%3E%20int%3A%0A%20%20%20%20%20%20%20%20return%2042%0A%0Aclass%20CallInt(metaclass%3DMeta2)%3A%0A%20%20%20%20pass%0A%0A%0AT%20%3D%20TypeVar(%22T%22)%0A%0Aclass%20Meta3(type)%3A%0A%20%20%20%20def%20 __call__ (self%3A%20type%5BT%5D%2C%20*args%3A%20Any%2C%20**kwargs%3A%20Any)%20-%3E%20T%3A%0A%20%20%20%20%20%20%20%20return%20super(). __call__ (*args%2C%20**kwargs)%0A%0Aclass%20CallSelf(metaclass%3DMeta3)%3A%0A%20%20%20%20pass%0A%0Areveal_type(CallAny())%0Areveal_type(CallInt())%0Areveal_type(CallSelf())%0A): Any, int, CallSelf

Pyright’s behavior here is consistent with the proposed spec, but I think pyre’s behavior is better.

---

<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:** [April 3, 2024, 8:40pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/17 "2024-04-03T20:40:49Z")

</div>

I don’t agree with removing the `Any` special case for ` __call__ `. As you said, it’s rare that a metaclass overrides ` __call__ ` in a way that would change the normal behavior of `type. __call__ `. Typically this is only done in cases where `NoReturn` is the intended return type. If we treat an `Any` return type as an indication that the metaclass is overriding the normal behavior, that creates big problems in the case where there is a metaclass ` __call__ ` method that happens to be unannotated. We’d need to differentiate between explicit `Any` and implicit `Any` (what pyright calls `Unknown`). That’s not a concept that’s found anywhere in the typing spec right now, and I don’t think this is the time to introduce it. I think the current proposal is preferable.

---

<div class="post-metadata">

**Author:** ![Jelle](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jelle/32/1049_2.png) [@Jelle](https://discuss.python.org/u/Jelle)\
**Post date:** [April 6, 2024, 1:32pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/18 "2024-04-06T13:32:58Z")

</div>

I see what you mean, but given that overriding ` __call__ ` in a metaclass is rare, I don’t think it is a big enough problem to warrant a special case. An unannotated ` __call__ ` would indeed make type checking more difficult, but there is a simple solution for users: add a type annotation.

---

<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:** [April 6, 2024, 3:56pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/19 "2024-04-06T15:56:01Z")

</div>

> [@Jelle](#):
>
> there is a simple solution for users: add a type annotation

That’s not an option if the class is in a library, which is most often where a developers will run into this issue. Developers have no recourse in this case — other than to file a bug with the library author and wait an indefinite time for the issue to be fixed and a new library to be released.

The current behavior of all type checkers is friendly to Python developers in this case. The change that you’re suggesting here would be hostile to developers. I don’t think that’s the right answer.

---

<div class="post-metadata">

**Author:** ![Jelle](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jelle/32/1049_2.png) [@Jelle](https://discuss.python.org/u/Jelle)\
**Post date:** [April 8, 2024, 3:20pm UTC](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744/20 "2024-04-08T15:20:04Z")

</div>

If the issue is in a library, users can provide stub files. That is no different from the situation with any other unannotated function.

My concern here is with keeping the type system easy to understand. I would find it confusing if I explicitly write that my ` __call__ ` method returns `Any`, and type checkers then interpret it as something else. Special cases like the one you propose can be helpful for usability, but they also hurt usability by making the entire system less predictable. In this case, I don’t think the use case is common enough to warrant a special case.

[Next page](https://discuss.python.org/t/draft-typing-spec-chapter-for-constructors/49744.md?page=2)
