3 ms·
Type hints can also be strings, which at least PyCharm resolves as if they were real type references from typing import List class Alpha: @stat
by mdaniel 4y ago
Type hints can also be strings, which at least PyCharm resolves as if they were real type references
from typing import List
class Alpha:
@staticmethod
def doit(b: "Beta") -> List["Beta"]:
return [b]
class Beta:
@staticmethod
def doit(a: "Alpha") -> List["Alpha"]:
return [a]
- squeaky-clean 4y agoThis only works if they're in the same file, or Alpha imports Beta later in the file and vice versa. Once you split these into separate files (say for example adding a gamma.py file and Gamma class that uses Alpha and Beta) you get Unresolved reference 'Alpha' and Unresolved reference 'Beta', and typechecking calls to Alpha.doit and Beta.doit from Gamma does not work.
- mdaniel 4y agoI thought it also accepted fully qualified type names but I just tried it and you're right. Relevant to this submission's title, if your packages are side-effect free, just importing the namespace then allows referencing the type names without touching the "actual" type import gamma def doit(bar: "gamma.Gamma"): ... In Java my answer to circular deps is the introduction of an interface that the concrete types can implement but then breaks the cycle