-
Notifications
You must be signed in to change notification settings - Fork 7
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
fix: refactor type hint retrieval #312
Conversation
@@ -732,6 +732,52 @@ def extract_product_name(name): | |||
return product_name | |||
|
|||
|
|||
def _extract_type_name(annotation: Any) -> str: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are annotations and type hints the same? If not, I'd rename annotation
to type_hint
, or maybe argument_annotations
. Feel free to Ack if this breaks your current naming conventions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's quite the same - annotations may hint at the type hint, however it's more like the object that includes the type hint. Probably best to stick to annotations
from https://docs.python.org/3/library/inspect.html#inspect.get_annotations.
type_name[end_index+prefix_end_len:], | ||
]) | ||
|
||
return type_name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please consider adding a unit test for this new logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me add a unit test as a followup PR. Filed #318.
@@ -732,6 +732,52 @@ def extract_product_name(name): | |||
return product_name | |||
|
|||
|
|||
def _extract_type_name(annotation: Any) -> str: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dict[Any]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, tried looking into the type and it's very messy lol it returns the inspected object's class directly. I will add this to the docstring!
FYI this is what I got from a small sample:
type: <class 'typing._CallableGenericAlias'>
type: <class 'typing._GenericAlias'>
type: <class 'typing._CallableGenericAlias'>
type: <class 'typing._GenericAlias'>
type: <class 'typing._CallableGenericAlias'>
type: <class 'typing._GenericAlias'>
type: <class 'typing._CallableGenericAlias'>
type: <class 'typing._GenericAlias'>
type: <class 'typing._CallableGenericAlias'>
type: <class 'typing._UnionGenericAlias'>
type: <class 'typing._CallableType'>
type: <class 'str'>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the review!
@@ -732,6 +732,52 @@ def extract_product_name(name): | |||
return product_name | |||
|
|||
|
|||
def _extract_type_name(annotation: Any) -> str: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's quite the same - annotations may hint at the type hint, however it's more like the object that includes the type hint. Probably best to stick to annotations
from https://docs.python.org/3/library/inspect.html#inspect.get_annotations.
@@ -732,6 +732,52 @@ def extract_product_name(name): | |||
return product_name | |||
|
|||
|
|||
def _extract_type_name(annotation: Any) -> str: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, tried looking into the type and it's very messy lol it returns the inspected object's class directly. I will add this to the docstring!
FYI this is what I got from a small sample:
type: <class 'typing._CallableGenericAlias'>
type: <class 'typing._GenericAlias'>
type: <class 'typing._CallableGenericAlias'>
type: <class 'typing._GenericAlias'>
type: <class 'typing._CallableGenericAlias'>
type: <class 'typing._GenericAlias'>
type: <class 'typing._CallableGenericAlias'>
type: <class 'typing._GenericAlias'>
type: <class 'typing._CallableGenericAlias'>
type: <class 'typing._UnionGenericAlias'>
type: <class 'typing._CallableType'>
type: <class 'str'>
type_name[end_index+prefix_end_len:], | ||
]) | ||
|
||
return type_name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me add a unit test as a followup PR. Filed #318.
🤖 I have created a release *beep* *boop* --- ## [2.0.6](https://github.com/googleapis/sphinx-docfx-yaml/compare/v2.0.5...v2.0.6) (2023-10-30) ### Bug Fixes * Refactor type hint retrieval ([#312](https://github.com/googleapis/sphinx-docfx-yaml/issues/312)) ([0c91f60](https://github.com/googleapis/sphinx-docfx-yaml/commit/0c91f60fb2eab8b184b1228d5f3eb133be7ae201)) * Support docstrings without type added ([#311](https://github.com/googleapis/sphinx-docfx-yaml/issues/311)) ([d214d64](https://github.com/googleapis/sphinx-docfx-yaml/commit/d214d64c54d06fa715535b14932ac1ad67fa0d09)) * Update annotation name extraction logic and add unit tests ([#320](https://github.com/googleapis/sphinx-docfx-yaml/issues/320)) ([220951d](https://github.com/googleapis/sphinx-docfx-yaml/commit/220951d341c27d345679f64fb67968320168e428)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
Refactors type hint retrieval when inspecting the source code. The indentation was very ugly and finally got to get around fixing it.
Given the annotation, tries to retrieve its name. For more complex types, removes ForwardRef references as they're used for type hinting, not for the user to read.
Updated goldens to verify the fix can be found in #311 - I split this PR into two for smaller PRs to review. Added goldens in this PR seem to not have been picked up from a previous PR, perhaps.
Towards b/296938464. Unblocks #311.