Python Forum
Function Annotation got NameError: name 'xxx' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function Annotation got NameError: name 'xxx' is not defined
#1
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:
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 = cls
Traceback (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 = cls
Same 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?
Reply


Messages In This Thread
Function Annotation got NameError: name 'xxx' is not defined - by Lance - Oct-22-2019, 07:36 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 303 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Decorators @ annotation drcl 3 410 Feb-24-2024, 06:12 AM
Last Post: Gribouillis
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 597 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,323 Sep-03-2023, 03:22 PM
Last Post: deanhystad
Question How to annotation type? gamecss 4 997 Jul-27-2023, 03:03 AM
Last Post: gamecss
  Getting NameError for a function that is defined JonWayn 2 1,115 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,907 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  How to print the output of a defined function bshoushtarian 4 1,318 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,348 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,518 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020