33
44import copy
55
6- from ._compat import get_generic_base
6+ from ._compat import PY_3_9_PLUS , get_generic_base
77from ._make import NOTHING , _obj_setattr , fields
88from .exceptions import AttrsAttributeNotFoundError
99
@@ -379,7 +379,9 @@ def evolve(inst, **changes):
379379 return cls (** changes )
380380
381381
382- def resolve_types (cls , globalns = None , localns = None , attribs = None ):
382+ def resolve_types (
383+ cls , globalns = None , localns = None , attribs = None , include_extras = True
384+ ):
383385 """
384386 Resolve any strings and forward annotations in type annotations.
385387
@@ -399,6 +401,10 @@ def resolve_types(cls, globalns=None, localns=None, attribs=None):
399401 :param Optional[list] attribs: List of attribs for the given class.
400402 This is necessary when calling from inside a ``field_transformer``
401403 since *cls* is not an *attrs* class yet.
404+ :param bool include_extras: Resolve more accurately, if possible.
405+ Pass ``include_extras`` to ``typing.get_hints``, if supported by the
406+ typing module. On supported Python versions (3.9+), this resolves the
407+ types more accurately.
402408
403409 :raise TypeError: If *cls* is not a class.
404410 :raise attrs.exceptions.NotAnAttrsClassError: If *cls* is not an *attrs*
@@ -411,14 +417,20 @@ class and you didn't pass any attribs.
411417
412418 .. versionadded:: 20.1.0
413419 .. versionadded:: 21.1.0 *attribs*
420+ .. versionadded:: 23.1.0 *include_extras*
414421
415422 """
416423 # Since calling get_type_hints is expensive we cache whether we've
417424 # done it already.
418425 if getattr (cls , "__attrs_types_resolved__" , None ) != cls :
419426 import typing
420427
421- hints = typing .get_type_hints (cls , globalns = globalns , localns = localns )
428+ kwargs = {"globalns" : globalns , "localns" : localns }
429+
430+ if PY_3_9_PLUS :
431+ kwargs ["include_extras" ] = include_extras
432+
433+ hints = typing .get_type_hints (cls , ** kwargs )
422434 for field in fields (cls ) if attribs is None else attribs :
423435 if field .name in hints :
424436 # Since fields have been frozen we must work around it.
0 commit comments