Erro in get_type_hints when registering text as module (bug in Python, fix pending) #88986

Closed
opened 2021-06-09 11:10:48 +02:00 by Joshua Knauber · 16 comments

System Information
Operating system: Windows-10-10.0.19041-SP0 64 Bits
Graphics card: Intel(R) Iris(R) Plus Graphics Intel 4.5.0 - Build 27.20.100.8681

Blender Version
Broken: version: 2.93.0, branch: master, commit date: 2021-06-02 11:21, hash: 84da05a8b8
Worked: 2.92

Short description of error
When using the as_module function on a script and running register blender throws an error:
image.png

Exact steps for others to reproduce the error
example.blend
Run the two commands specified in the text file to get the module and register it. I modified the Simple Operator template to make it as simple as possible.

This worked for me in 2.92 for registering and unregistering generated addons dynamically. While it still seems to register in 2.93 operators throw this error. They do appear to be registered when you use them in buttons for example but the property ('test' in the example file) isn't found.

**System Information** Operating system: Windows-10-10.0.19041-SP0 64 Bits Graphics card: Intel(R) Iris(R) Plus Graphics Intel 4.5.0 - Build 27.20.100.8681 **Blender Version** Broken: version: 2.93.0, branch: master, commit date: 2021-06-02 11:21, hash: `84da05a8b8` Worked: 2.92 **Short description of error** When using the `as_module` function on a script and running `register` blender throws an error: ![image.png](https://archive.blender.org/developer/F10163978/image.png) **Exact steps for others to reproduce the error** [example.blend](https://archive.blender.org/developer/F10163984/example.blend) Run the two commands specified in the text file to get the module and register it. I modified the Simple Operator template to make it as simple as possible. This worked for me in 2.92 for registering and unregistering generated addons dynamically. While it still seems to register in 2.93 operators throw this error. They do appear to be registered when you use them in buttons for example but the property ('test' in the example file) isn't found.
Author

Added subscriber: @JoshuaKnauber

Added subscriber: @JoshuaKnauber

#89325 was marked as duplicate of this issue

#89325 was marked as duplicate of this issue

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'

Added subscriber: @WCN

Added subscriber: @WCN

This may have been fixed upstream:

https://bugs.python.org/issue41515

This may have been fixed upstream: https://bugs.python.org/issue41515

For the other users in this thread:

I think I've managed to patch this issue out of my Blender, by going to 2.93/python/lib/python3.9/typing.py in my Blender install directory and changing the one line below.

Change line 1408 from:

base_globals = sys.modules[base.__module__].__dict__

to

base_globals = getattr(sys.modules.get(base.__module__, None), '__dict__', {})

This doesn't seem to have any side effects, and I don't think it really should have any side effects, as it should only modify the code path in conditions where it would otherwise result in an error.


On Blender source code itself:

I think passing an empty dictionary for globalsn to typing.get_type_hints(cls, globalsn={}) in Blender may be enough to get the previous behaviour:

https://docs.python.org/release/3.9.5/whatsnew/changelog.html#id272

It seems to work for getting annotations from the class in the console panel, anyway.

I'm not sure the change upstream is actually going to fix this either. It looks like the new default behaviour is that classes defined in a synthetic module will just be skipped, and not have their annotations collected at all:

https://github.com/python/cpython/pull/25352/files#diff-ddb987fca5f5df0c9a2f5521ed687919d70bb3d64eaeb8021f98833a2a716887

So it may be necessary to pass an empty dict for globalsn even after the upstream fix.


Found the commit where the change was introduced:

f350a268a7 (diff-ddb987fca5f5df0c9a2f5521ed687919d70bb3d64eaeb8021f98833a2a716887R1501)

**For the other users in this thread:** I think I've managed to patch this issue out of my Blender, by going to `2.93/python/lib/python3.9/typing.py` in my Blender install directory and changing the one line below. Change line 1408 from: `base_globals = sys.modules[base.__module__].__dict__` to `base_globals = getattr(sys.modules.get(base.__module__, None), '__dict__', {})` This doesn't seem to have any side effects, and I don't think it really should have any side effects, as it should only modify the code path in conditions where it would otherwise result in an error. --- **On Blender source code itself:** I think passing an empty dictionary for `globalsn` to `typing.get_type_hints(cls, globalsn={})` in Blender may be enough to get the previous behaviour: https://docs.python.org/release/3.9.5/whatsnew/changelog.html#id272 It seems to work for getting annotations from the class in the console panel, anyway. I'm not sure the change upstream is actually going to fix this either. It looks like the new default behaviour is that classes defined in a synthetic module will just be skipped, and not have their annotations collected at all: https://github.com/python/cpython/pull/25352/files#diff-ddb987fca5f5df0c9a2f5521ed687919d70bb3d64eaeb8021f98833a2a716887 So it may be necessary to pass an empty `dict` for `globalsn` even after the upstream fix. --- Found the commit where the change was introduced: https://github.com/python/cpython/commit/f350a268a7071ce7d7a5bb86a9b1229782d4963b#diff-ddb987fca5f5df0c9a2f5521ed687919d70bb3d64eaeb8021f98833a2a716887R1501

I opened an issue upstream, for the Python standard library:

https://bugs.python.org/issue44468

If they decide to make the new change suggested, then I think that should also fix this issue.

Maybe it'd still be better to explicitly pass an empty dict though, to avoid relying on opaque behaviour.

I opened an issue upstream, for the Python standard library: https://bugs.python.org/issue44468 If they decide to make the new change suggested, then I think that should also fix this issue. Maybe it'd still be better to explicitly pass an empty `dict` though, to avoid relying on opaque behaviour.
Campbell Barton changed title from Erro in get_type_hints when registering text as module to Erro in get_type_hints when registering text as module (bug in Python, fix pending) 2021-06-23 09:29:19 +02:00

I submitted a fix upstream, which has now been merged:

https://github.com/python/cpython/pull/26862

It's been backported to the 3.10 branch, so I guess it will make it into the next release.

I submitted a fix upstream, which has now been merged: https://github.com/python/cpython/pull/26862 It's been backported to the 3.10 branch, so I guess it will make it into the next release.

Still in Python 3.9.6. There doesn't seem to be any backport for version numbers below 3.10.

Just curious: What is the first version of Blender planned to ship with Python 3.10? (Until then I will have to manually patch the static program files.)

Still in Python 3.9.6. There doesn't seem to be any backport for version numbers below 3.10. Just curious: What is the first version of Blender planned to ship with Python 3.10? (Until then I will have to manually patch the static program files.)
Author

Is there any update on this? I guess this is fixed with python 3.10 to get back to Will's question of when this version will be in blender?

Is there any update on this? I guess this is fixed with python 3.10 to get back to Will's question of when this version will be in blender?

Added subscriber: @ispaure

Added subscriber: @ispaure

Hi, I also need this for my code (and have needed to patch the program files since this summer (typing.py) to fix this). Would appreciate any update on when it will be implemented or how to fix my current issue.

def import_python_module_absolute_path(path):

  """
  Import python file at absolute path.
  """
  if os.path.exists(path):
      spec = importlib_util.spec_from_file_location('module', path)
      foo = importlib.util.module_from_spec(spec)
      spec.loader.exec_module(foo)
      return foo
  else:
      return None

I am thinking my function here is the culprit, but unsure how I need to change it to bypass this issue. Any help very much appreciated. Thanks :)

Additional note: It's when I try to register this created class with bpy.utils.register_class(foo) that I get the error, which didn't happen before Blender switched python version sometime in late 2020 or early 2021.

Hi, I also need this for my code (and have needed to patch the program files since this summer (typing.py) to fix this). Would appreciate any update on when it will be implemented or how to fix my current issue. def import_python_module_absolute_path(path): ``` """ Import python file at absolute path. """ if os.path.exists(path): spec = importlib_util.spec_from_file_location('module', path) foo = importlib.util.module_from_spec(spec) spec.loader.exec_module(foo) return foo else: return None ``` I am thinking my function here is the culprit, but unsure how I need to change it to bypass this issue. Any help very much appreciated. Thanks :) Additional note: It's when I try to register this created class with bpy.utils.register_class(foo) that I get the error, which didn't happen before Blender switched python version sometime in late 2020 or early 2021.

@ispaure The SVN files for Blender 3.1 have a patched Python 3.10 version in them, so hopefully that will bring the fix to more users.

https://svn.blender.org/svnroot/bf-blender/tags/blender-3.1-release/lib/win64_vc15/python/
https://svn.blender.org/svnroot/bf-blender/tags/blender-3.1-release/lib/win64_vc15/python/310/lib/typing.py

IIRC the problem is a result of synthetic modules: When a module is constructed from a string, file, or some such instead of the usual import mechanisms, the classes in it don't have a .__module__ attribute valid for sys.modules- [ ], so Python 3.9 skips evaluating their type hints for subclasses.

I suppose if you want to "fix" that function, you would probably look into filling in .__module__ in the classes and sys.modules with a synthetic module name:

E.G.:

modulename = f"FakeModule-{path}"
for obj in foo.__dict__.values();
	if isinstance(obj, type) or hasattr(obj, '__module__'):
		obj.__module__ = modulename
		- Also recurse for nesteds if needed.
		- Hm… May AttributeError if you have any slots-y, immutable, or built-in classes in the module top level. Just wrap in a "try" if so.
sys.modules[modulename] = foo
foo.__name__ = modulename #  Not needed, but may as well be thorough.
return foo

Not sure if this the above will be enough. It's been a while since I've looked at this.

Personally I haven't run into this issue since 3.0, though I think that's because my OS's Blender package links to system Python3.10.

@ispaure The SVN files for Blender 3.1 have a patched Python 3.10 version in them, so hopefully that will bring the fix to more users. https://svn.blender.org/svnroot/bf-blender/tags/blender-3.1-release/lib/win64_vc15/python/ https://svn.blender.org/svnroot/bf-blender/tags/blender-3.1-release/lib/win64_vc15/python/310/lib/typing.py IIRC the problem is a result of synthetic modules: When a module is constructed from a string, file, or some such instead of the usual import mechanisms, the classes in it don't have a `.__module__` attribute valid for `sys.modules- [ ]`, so Python 3.9 skips evaluating their type hints for subclasses. I suppose if you want to "fix" that function, you would probably look into filling in `.__module__` in the classes and `sys.modules` with a synthetic module name: E.G.: ``` modulename = f"FakeModule-{path}" for obj in foo.__dict__.values(); if isinstance(obj, type) or hasattr(obj, '__module__'): obj.__module__ = modulename - Also recurse for nesteds if needed. - Hm… May AttributeError if you have any slots-y, immutable, or built-in classes in the module top level. Just wrap in a "try" if so. sys.modules[modulename] = foo foo.__name__ = modulename # Not needed, but may as well be thorough. return foo ``` Not sure if this the above will be enough. It's been a while since I've looked at this. Personally I haven't run into this issue since 3.0, though I think that's because my OS's Blender package links to system Python3.10.

Added subscriber: @ideasman42

Added subscriber: @ideasman42

This is no longer a problem for Python 3.10 & Blender 3.1, closing.

This is no longer a problem for Python 3.10 & Blender 3.1, closing.

Changed status from 'Confirmed' to: 'Resolved'

Changed status from 'Confirmed' to: 'Resolved'
Campbell Barton self-assigned this 2022-03-30 11:13:12 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
6 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#88986
No description provided.