8000 Ruggedize cmd2's runtime type annotation validation by tleonhardt · Pull Request #1442 · python-cmd2/cmd2 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Ruggedize cmd2's runtime type annotation validation #1442

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

Merged
merged 6 commits into from
Jun 8, 2025

Conversation

tleonhardt
Copy link
Member
@tleonhardt tleonhardt commented Jun 8, 2025

Change cmd2's runtime type annotation validation for hook callback function registration to be based on typing.get_type_hints

Previously it was based on inspect.signature. The problem is that prior to Python 3.10, the inspect module doesn't have a safe way of evaluating type annotations that works equivalently both in the presence or absence of from __future__ import annotations. Hence, any attempt at using that in an app would break cmd2. Even in Python 3.10+, it requires an extra argument to be provided.

This change adds a get_types() helper function to the cmd2.utils module which uses typing.get_type_hints() to do the introspection in a safer way.

Down the road when cmd2 drops support for Python 3.9, we can evaluate to switching the underlying implementation to use inspect.get_annotations() if we wish.

Closes #1443

…g.get_type_hints

Previously it was based on inspect.signature. The problem is that to Python 3.10, the inspect module doesn't have a safe way of evaluating type annotations that works equivalently both in the presence or absence of "from __future__ import annotations". Hence, any attempt at using that in an app would break cmd2.

This change adds a get_types() helper function to the cmd2.utils module which uses typing.get_type_hints() to do the introspection in a safer way.
Copy link
codecov bot commented Jun 8, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.55%. Comparing base (8d1c82c) to head (896c7bb).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1442      +/-   ##
==========================================
- Coverage   98.56%   98.55%   -0.02%     
==========================================
  Files          20       20              
  Lines        5726     5742      +16     
==========================================
+ Hits         5644     5659      +15     
- Misses         82       83       +1     
Flag Coverage Δ
unittests 98.55% <100.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tleonhardt tleonhardt self-assigned this Jun 8, 2025
@tleonhardt tleonhardt added the bug label Jun 8, 2025
@tleonhardt tleonhardt merged commit 9eaea96 into master Jun 8, 2025
29 of 30 checks passed
@tleonhardt tleonhardt deleted the typing-get-type-hints branch June 8, 2025 22:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cmd2 crashes when from __future__ import annotations is used
1 participant
0