Skip to content

Support attributes and type aliases #18

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 7 commits into from
Oct 10, 2024
Merged

Support attributes and type aliases #18

merged 7 commits into from
Oct 10, 2024

Conversation

lagru
Copy link
Member

@lagru lagru commented Oct 7, 2024

For now, docstub relies on static analysis only. Therefore, it is very difficult to differentiate a type alias such as x = str from a regular assignment statement. However, explicit ones are easier to detect and they should be preserved as is (closes #17).

This PR introduces support for several things:

  • Collect explicit type aliases during static analysis. Make sure that x: TypeAlias = str and type x = str are collected as importable type annotations .

  • Fallback to typing.Any for all un-annotated attributes except in type aliases.

  • Replace right side of assignments with ... except in type aliases.

  • Expand assignment statements with multiple targets on the left side, with multiple annotated ones. E.g. x, y = (3, 4) to x: Any = ...; y: Any = ....

While it is very difficult to statically detect a type alias [1] such as
`x = str`, explicit ones are easier to detect. This change makes sure
that `x: TypeAlias = str` and `type x = str` are collected as importable
type annotations.

[1] https://docs.python.org/3/library/typing.html#type-aliases
@lagru lagru added the enhancement New feature or request label Oct 7, 2024
@lagru lagru added this to the v0.3 milestone Oct 7, 2024
@lagru lagru mentioned this pull request Oct 7, 2024
28 tasks
Type information in docstrings is not yet taken into account. Instead,
`typing.Any` is used as a fallback.
@lagru lagru changed the title Support collection of explicit type aliases Support attributes and type aliases Oct 8, 2024
lagru added 5 commits October 9, 2024 20:45
as well as proper handling in context of stubs. In stubs, the value
should basically always be omitted from attributes except for a few
exceptions.
Type information in docstrings is not yet taken into account. Instead,
`typing.Any` is used as a fallback.
@lagru lagru marked this pull request as ready for review October 9, 2024 19:00
@lagru lagru merged commit 3bd33e4 into main Oct 10, 2024
4 checks passed
@lagru lagru deleted the support-attributes branch October 10, 2024 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Figure out how to support type aliases
1 participant