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

fix: remove runtime assignment of type aliases to Any. #2378

Merged
merged 5 commits into from
Sep 29, 2023

Conversation

peterschutt
Copy link
Contributor

@peterschutt peterschutt commented Sep 28, 2023

This PR replaces this pattern:

from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
    from typing_extensions import TypeAlias

    from litestar import Thing

    ThingAlias: TypeAlias = Thing
else:
    ThingAlias: TypeAlias = Any

My best guess is that this pattern was some workaround for circular dependencies before we introduced __future__.annotations.

I believe these changes will be beneficial as:

  • the current pattern is non-standard (at least in my experience)
  • the current pattern causes the types to be documented as Any in the API docs.

Example of current API docs for alias types:

image

Pull Request Checklist

  • New code has 100% test coverage
  • (If applicable) The prose documentation has been updated to reflect the changes introduced by this PR
  • (If applicable) The reference documentation has been updated to reflect the changes introduced by this PR

Description

Close Issue(s)

This PR replaces this pattern:

```py
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
    from typing_extensions import TypeAlias

    from litestar import Thing

    ThingAlias: TypeAlias = Thing
else:
    ThingAlias: TypeAlias = Any
```

My best guess is that this pattern was some workaround for circular dependencies before we introduced `__future__.annotations`.

I believe these changes will be beneficial as:
- the current pattern is non-standard (at least in my experience)
- the current pattern causes the types to be documented as `Any` in the API docs.
@peterschutt
Copy link
Contributor Author

peterschutt commented Sep 28, 2023

Type API docs improve somewhat:

image

However, its just treating it as a string constant and using docstring for str. Any @litestar-org/maintainers or @litestar-org/members know how to prevent this?

litestar/types/internal_types.py Outdated Show resolved Hide resolved
De-stringizes type aliases where possible by importing components from typing at runtime.
@peterschutt peterschutt marked this pull request as ready for review September 28, 2023 23:41
@peterschutt peterschutt requested review from a team as code owners September 28, 2023 23:41
@sonarcloud
Copy link

sonarcloud bot commented Sep 28, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

@github-actions
Copy link

Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/2378

@peterschutt peterschutt merged commit 82aeb3d into main Sep 29, 2023
18 checks passed
@peterschutt peterschutt deleted the fix-runtime-type-aliases branch September 29, 2023 00:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants