0

I have a dataclass in extensions > __init__.py like:

@dataclass
class Range:
    start: date
    end: date

    def overlap(self, other: Range) -> bool:
      # method body removed

My IDE is complaining about the method parameter

Unresolved reference 'Range' 

If I change the parameter type to

def overlap(self, other: extensions.Range) -> bool:

IDE stops complaining but when I run my program I get

partially initialized module 'extensions' has no attribute 'Range' (most likely due to a circular import)

I'm learning Typing in python and cannot figure out how to define this type in my method

TheTechRobo the Nerd
  • 1,249
  • 15
  • 28
Vahid
  • 1,625
  • 1
  • 18
  • 33
  • 1
    Or if you're on Python 3.11 or greater, you can use [`typing.Self`](https://docs.python.org/3/library/typing.html#typing.Self) – Axe319 Dec 15 '22 at 17:15

1 Answers1

0

I think this has been asked before. You can use a forward reference in the form of a string.

@dataclass
class Range:
    start: date
    end: date

    def overlap(self, other: 'Range') -> bool:
        pass
  • 1
    Please don't answer the question, if you have already found a duplicate. Just flag the question as duplicate. If you have something to add to existing answers, post your answer on the duplicate target. – STerliakov Dec 18 '22 at 19:44