Python typing signature for instance of subclass?

Consider:

from __future__ import annotations

class A:
    @classmethod
    def get(cls) -> A:
        return cls()

class B(A):
    pass

def func() -> B: # Line 12
    return B.get()

Running mypy on this we get:

$ mypy test.py
test.py:12: error: Incompatible return value type (got "A", expected "B")
Found 1 error in 1 file (checked 1 source file)

Additionally, I have checked to see if old-style recursive annotations work. That is:

# from __future__ import annotations

class A:
    @classmethod
    def get(cls) -> "A":
# ...

...to no avail.

Of course one could do:

from typing import cast

def func() -> B: # Line 12
    return cast(B, B.get())

Every time this case pops up. But I would like to avoid doing that.

How should one go about typing this?



from Recent Questions - Stack Overflow https://ift.tt/3iYs4SB
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations