Florian Bruhin <python....@the-compiler.org> added the comment:

Ah, I wasn't aware of that, thanks for the pointer! So what inspect does 
internally is:

    def _get_type_hints(func, **kwargs):
        try:
            return typing.get_type_hints(func, **kwargs)
        except Exception:
            # First, try to use the get_type_hints to resolve
            # annotations. But for keeping the behavior intact
            # if there was a problem with that (like the namespace
            # can't resolve some annotation) continue to use
            # string annotations
            return func.__annotations__

Which means there's even some "prior art" there already falling back to a 
string when the annotation couldn't be resolved. Doing so in 
typing.get_type_hints on a per-argument basis would thus also make inspect more 
consistent:

Right now,

    print(repr(inspect.signature(fun).parameters['b'].annotation))

in my example returns a string, but when changing the annotation for `a`, the 
returned annotation for `b` is now magically a `typing.Union` object.

(I personally would indeed expect inspect to resolve those annotations, but 
yeah, let's keep that in issue43355.)

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue43463>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to