This PR forces the use of from __future__ import annotations throughout cmd2#1444
This PR forces the use of from __future__ import annotations throughout cmd2#1444tleonhardt wants to merge 10 commits intomasterfrom
from __future__ import annotations throughout cmd2#1444Conversation
…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.
… for this change I first verified that this test fails on the current master branch
…ype hints that don't require things like Optional This also supports better runtime performance by allowing imports only used for type hints to be moved under `if TYPE_CHECKING:` blocks.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1444 +/- ##
==========================================
+ Coverage 98.55% 98.57% +0.02%
==========================================
Files 20 20
Lines 5742 5753 +11
==========================================
+ Hits 5659 5671 +12
+ Misses 83 82 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
|
@kmvanbrunt If you get a chance, I'd appreciate some help with manually testing this one. It seems like a good change to me, but it a large and subtle one. I want to make sure it doesn't negatively impact things like type awareness in IDEs and such. |
|
I did some experimentation on a large codebase I work on and found that enabling Given that, I think this change would be inherently risky. So I am closing this PR. |
This PR forces the use of
from __future__ import annotationsthroughoutcmd2.Advantages of this include:
Optional, can use| NoneinsteadUnionin type hints, can use stuff likeint | strinsteadUnionfor things like casts and type vars for versions prior to Python 3.10if TYPE_CHECKING:blockThe only real disadvantage I can see if we will probably need to leave that import in place until the oldest version we support is Python 3.14 since that is when lazy eval of type annotations is in Python by default. Either that or we might need to restore quotes around types in a couple places where they were used for forward references.
NOTE: Nearly all of the changes in this PR were done automatically by
ruffafter I changed the configuration forruffinpyproject.toml.