I am introducing function annotation in my coding, but found both python and Pycharm will validate if the type in function annotation is valid, if a none defined type is used, it will raise error. It's different with the statement in PEP 3107: "By itself, Python does not attach any particular meaning or significance to annotations. " (https://www.python.org/dev/peps/pep-3107...nnotations). This will be very boring in case of some circular reference situation, eg. below coding will get warning hint from Pycharm and get error when running:
File "Module1.py", line 1, in <module>
class Cls1:
File "Module1.py", line 5, in Cls1
def setSibling(self, cls: Cls2):
NameError: name 'Cls2' is not defined
Alternatively, I have to use python 2 style type remarks as below, and it works.
class Cls1:
def __init__(self):
self.sibling:Cls2 = None
def setSibling(self, cls: Cls2):
self.sibling = cls
class Cls2:
def __init__(self):
self.sibling: Cls1 = None
def setSibling(self, cls: Cls1):
self.sibling = clsTraceback (most recent call last):File "Module1.py", line 1, in <module>
class Cls1:
File "Module1.py", line 5, in Cls1
def setSibling(self, cls: Cls2):
NameError: name 'Cls2' is not defined
Alternatively, I have to use python 2 style type remarks as below, and it works.
class Cls1:
def __init__(self):
self.sibling:Cls2 = None
def setSibling(self, cls):
# type: (Cls2)
self.sibling = cls
class Cls2:
def __init__(self):
self.sibling: Cls1 = None
def setSibling(self, cls):
#type: (Cls1)
self.sibling = clsSame with normal coding logic, the type hint inside a function allows type defined afterward but the type in function annotation must be defined in advance, but in circular reference situation, it's almost impossible. Does python 3 function annotation isn't fully complied with PEP 3107 statement or there is other thing which I don't know?
