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
#5
(Oct-23-2019, 01:39 AM)ichabod801 Wrote: You can do the circular references within a function because the code within the function is not executed until you call the function. At that point the relevant names have been defined. I don't think you can get around this. The only thing I can think of is to define a dummy version of Cls2, then Cls1 with the Cls2 annotation, then Cls2 with the Cls1 annotation. But with the way names work in Python, I expect that the annotation in Cls1 will still point to the evaluated dummy Cls2. Another idea is to have Cls1 and Cls2 both inherit from Cls0, and use Cls0 as the annotation. Not as precise as you want, but it's something. Doing some more research, it appears PEP 583 calls for delayed evaluation of annotations. This was implemented in 3.7 and can be imported from __future__ as annotations. Otherwise PEP 484 recommends using a string of the expression, which should be evaluated once the module is fully loaded.

I tried dummy Cls2 solution, that could 'cheat' python interpreter but Pycharm still refers Cls2 to the dummy one, so it could not help for IDE typing hint which is biggest value for me now. Cls0 solution has same issue.

'__future__' looks like the proper solution, which is more like static language mechanism to load all type definition in advance, I will study and try it. But Do you think it will be much better if python interpreter stick to the basic statement of PEP3107 not to evaluate annotation expressions and let static validation tool like mypy to do the static validation?
Reply


Messages In This Thread
RE: Function Annotation got NameError: name 'xxx' is not defined - by Lance - Oct-23-2019, 02:28 AM

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