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

Use get_typing_hints instead of __annotations__ to resolve types in Python 3.10 #297

Closed
wants to merge 5 commits into from
Closed

Conversation

tirkarthi
Copy link
Contributor

What:

Fixes #296

Why:

Due to PEP 563 becoming default in Python 3.10 the __annotations__ value doesn't store types and it stores string values. Using typing.get_type_hints ensures the types are evaluated and returned like behavior before Python 3.10 .

How:

Risks:

Checklist:

  • Added tests, if you've added code that should be tested
  • Updated the documentation, if you've changed APIs
  • Ensured the test suite passes
  • Made sure your code lints
  • Completed the Contributor License Agreement ("CLA")

@facebook-github-bot facebook-github-bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Apr 7, 2021
@deathowl
Copy link
Member

deathowl commented Apr 7, 2021

Hey @tirkarthi , Thanks for the PR, please make format , otherwise it looks good, we'll add a Python 3.10 Target to our builds

@tirkarthi tirkarthi marked this pull request as ready for review April 7, 2021 15:50
@tirkarthi
Copy link
Contributor Author

Thanks @deathowl I have fixed the formatting issues. I will wait if someone has issues on using get_type_hints by default to emulate behavior before Python 3.10 and https://www.python.org/dev/peps/pep-0563/#backwards-compatibility

@coveralls
Copy link

coveralls commented Apr 7, 2021

Pull Request Test Coverage Report for Build 748441995

  • 4 of 6 (66.67%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.07%) to 94.092%

Changes Missing Coverage Covered Lines Changed/Added Lines %
testslide/strict_mock.py 4 6 66.67%
Totals Coverage Status
Change from base Build 708713380: -0.07%
Covered Lines: 2564
Relevant Lines: 2725

💛 - Coveralls

@facebook-github-bot
Copy link

@deathowl has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

if hasattr(self._template, "__annotations__"):
annotations = self._template.__annotations__
if self._template is not None:
annotations = get_type_hints(self._template)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not play nice with pybind11 based modules

in get_type_hints
    base_globals = sys.modules[base.__module__].__dict__
KeyError: 'pybind11_builtins'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this seems to have caused an issue in sphinx as well where they catch keyerror and have empty annotations. I don't have the right setup but it will be good to know what was the value of annotations using __annotations__ attribute in earlier versions.

sphinx-doc/sphinx#8084

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess catching key error and returning empty annotations would work well as a workaround for us as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will make the changes. On further search seems this is an open issue in CPython where there is a PR open to catch the KeyError and discard it for these type of cases : https://bugs.python.org/issue41515

@facebook-github-bot
Copy link

@deathowl has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

annotations = get_type_hints(self._template)
try:
annotations = get_type_hints(self._template)
except KeyError:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we do except Exception just to make sure we dont break tests when we fail to get typehints for any funny modules?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KeyError seemed more specific to the modules with different __module__ . Are there any other failures that expose a bug with get_type_hints? I couldn't see the test failures since it's private.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@facebook-github-bot
Copy link

@deathowl has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link

@deathowl merged this pull request in 4499047.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Do not delete this pull request or issue due to inactivity. Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

patch_attribute issue with Python 3.10
4 participants