retype icon indicating copy to clipboard operation
retype copied to clipboard

Class not found in source with @type_check_only

Open ilai-deutel opened this issue 4 years ago • 2 comments

Hello,

I think that classes decorated with @type_check_only should be allowed even if they are not present in the source file (since they are not available at runtime). Using them currently results in a Class not found in source error:

foo.pyi:

from typing import type_check_only
from typing_extensions import Protocol

@type_check_only
class A(Protocol):
    def __call__(self, u: str) -> int: ...

def f(a: A) -> int: ...

foo.py:

def f(a):
    return a(u='abc')

retype outputs error: /path/to/foo.py: Class 'A' not found in source.

I was expecting an output similar to this:


from typing import TYPE_CHECKING
from typing_extensions import Protocol

if TYPE_CHECKING:
    class A(Protocol):
        def __call__(self, u: str) -> int: ...

def f(a: 'A') -> int:
    return a(u='abc')

ilai-deutel avatar Oct 11 '19 06:10 ilai-deutel

I think this makes sense, @ambv can confirm for sure. Can you create a PR with the proposed solution?

gaborbernat avatar Oct 11 '19 08:10 gaborbernat

I am working on a PR!

ilai-deutel avatar Oct 16 '19 08:10 ilai-deutel

This project is being retired and archived, so we'll no longer address this.

gaborbernat avatar Aug 14 '22 07:08 gaborbernat