baxx
baxx

Reputation: 4715

How to typehint custom classes with mypy

Given the following few modules:

# mod_1.py
from dataclasses import dataclass

@dataclass
class C:
    x : int
# mod_2.py
from mod_1 import C

c = C(x = 3)
# mod_3.py
from mod_2 import c

def f(something : <type c>) -> None:
    print(c.x)

I'm wondering what should be put in place of <type c>

Upvotes: 0

Views: 482

Answers (1)

chepner
chepner

Reputation: 531708

You can use a string if you don't import the type itself.

def f(something: 'C') -> None:
    print(c.x)

You can also import the type:

from mod_2 import c, C

def f(something: C) -> None:
    print(c.x)

Upvotes: 1

Related Questions