Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive warning (?) #163

Closed
A5rocks opened this issue Mar 9, 2021 · 1 comment
Closed

False positive warning (?) #163

A5rocks opened this issue Mar 9, 2021 · 1 comment

Comments

@A5rocks
Copy link

A5rocks commented Mar 9, 2021

Either this is a false positive, or evaluation of generated code's forward references is broken -- I really hope it's the former.

Minimal case:
repro.py:

from __future__ import annotations

import attr
import typing

@attr.s(auto_attribs=True)
class Example:
    blah: typing.Type[int]

Console output:

...
WARNING: Cannot resolve forward reference in type annotations of "repro.Example": name 'typing' is not defined
...

However, the docs still render alright (?) and if you use intersphinx it does work... So, mixed feelings from this. I may have messed up with browser cache or something, try this out and see for yourself.

Note that you can either comment out @attr.s(auto_attribs=True), remove auto_attribs=True (for some reason?), or comment out from __future__ import annotations to get rid of this warning.

I'm new to this whole documentation thing, so I may have missed something though.

@A5rocks
Copy link
Author

A5rocks commented Mar 15, 2021

More info:

  • You can typing.get_type_hints from another module, here's an example of how:
from __init__ import Example
from typing import get_type_hints

print(get_type_hints(Example))
  • Stack trace:
Traceback (most recent call last):
 File "C:\Users\A5rocks\Documents\fun\python-tests\docs\source\sphinx_autodoc_typehints.py", line 230, in get_all_type_hints
   rv = get_type_hints(obj)
 File "C:\Python39\lib\typing.py", line 1442, in get_type_hints
   value = _eval_type(value, globalns, localns)
 File "C:\Python39\lib\typing.py", line 277, in _eval_type
   return t._evaluate(globalns, localns, recursive_guard)
 File "C:\Python39\lib\typing.py", line 533, in _evaluate
   eval(self.__forward_code__, globalns, localns),
 File "<string>", line 1, in <module>
NameError: name 'typing' is not defined

I cannot think of a reason why this would work in one context and not in another....

Okay, so the obj that is erroring is the __init__ method, whereas in my example it's the whole class. Now this is starting to make some sense...

Looks like this is an attrs issue / will be fixed in the next version, whoops! python-attrs/attrs#760

@A5rocks A5rocks closed this as completed Mar 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant