英文:
Evaluating forward references with typing.get_type_hints in Python for a class defined inside another method/class
问题
我在调用具有字符串形式的前向引用的类的typing.get_type_hints()
时遇到了问题。我的代码在不在函数内部定义时可以正常工作。我在下面的Python 3.10中复制了一个最小示例:
import typing
class B:
pass
class A:
some_b: "B"
print(typing.get_type_hints(A)) # 打印 {'some_b': <class '__main__.B'>}
import typing
def func():
class B:
pass
class A:
some_b: "B"
print(typing.get_type_hints(A))
func() # NameError: name 'B' is not defined
这是否是预期行为?是否有任何方法可以解决这个问题,并确保字符串的前向引用在正确的范围内计算?
(Note: The code provided contains HTML entities for quotes and angle brackets, which may need to be corrected for proper execution in Python.)
英文:
I'm having trouble calling typing.get_type_hints() for classes that have forward references as strings. My code works with not defined inside of a function. I've reproduced a minimal example below in Python 3.10:
import typing
class B:
pass
class A:
some_b: "B"
print(typing.get_type_hints(A)) # prints {'some_b': <class '__main__.B'>}
import typing
def func():
class B:
pass
class A:
some_b: "B"
print(typing.get_type_hints(A))
func() # NameError: name 'B' is not defined
Is this expected behavior? Is there any way to get around this, and make sure that forward references with strings get evaluated in the correct scope?
答案1
得分: 2
typing.get_type_hints
允许您通过 localns
参数明确传递本地命名空间以解析引用。
from typing import get_type_hints
def func():
class A:
some_b: "B"
class B:
pass
print(get_type_hints(A, localns=locals()))
func()
输出:{'some_b': <class '__main__.func.<locals>.B'>}
请参阅 locals
文档。
顺便提一下:通过利用注解的延迟评估(PEP 563),您可以省略引号:
from __future__ import annotations
from typing import get_type_hints
def func():
class A:
some_b: B
class B:
pass
print(get_type_hints(A, localns=locals()))
英文:
typing.get_type_hints
allows you to explicitly pass the local namespace to use for resolving references via the localns
parameter.
from typing import get_type_hints
def func():
class A:
some_b: "B"
class B:
pass
print(get_type_hints(A, localns=locals()))
func()
Output: {'some_b': <class '__main__.func.<locals>.B'>}
See the docs for locals
.
Side note: By utilizing postponed evaluation of annotations (PEP 563) you can omit the quotation marks:
from __future__ import annotations
from typing import get_type_hints
def func():
class A:
some_b: B
class B:
pass
print(get_type_hints(A, localns=locals()))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论