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

Should we vendor typing_extensions.NotRequired? #1337

Open
dimaqq opened this issue Aug 26, 2024 · 8 comments
Open

Should we vendor typing_extensions.NotRequired? #1337

dimaqq opened this issue Aug 26, 2024 · 8 comments
Labels
25.04 investigate Needs further investigation - do we want to do this?

Comments

@dimaqq
Copy link
Contributor

dimaqq commented Aug 26, 2024

The code is really simple:

https://github.com/python/typing_extensions/blob/70cec91bec65155dc339d631ede2a933582558df/src/typing_extensions.py#L2220-L2256

We're using:

  • Required in ops/charm.py
  • NotRequired in ops/pebble.py
  • NotRequired in test/fake_pebble.py
@dimaqq dimaqq added the investigate Needs further investigation - do we want to do this? label Aug 26, 2024
@benhoyt
Copy link
Collaborator

benhoyt commented Aug 26, 2024

What's the motivation for this? Are they being removed from typing_extensions, or is there some other reason?

@dimaqq
Copy link
Contributor Author

dimaqq commented Aug 26, 2024

The motivation is to avoide the if TYPE_CHECKING: block.

The Required/NotRequired are used in TypedDict definitions, which means that TypedDict's can't be used without quotes. Granted, those typed dicts are internal.

I think it may partially help with the following comment for public typed dicts:
# In Python 3.11+ 'services' and 'labels' should be NotRequired, and total=True.

The latter could be an improvement for our library users, maybe?

Possibly related: #1336

@benhoyt benhoyt added the 25.04 label Sep 27, 2024
@benhoyt
Copy link
Collaborator

benhoyt commented Sep 27, 2024

We're uncertain whether this will actually work once they're vendored with Pyright. Investigate on a rainy day next cycle.

@james-garner-canonical
Copy link
Contributor

james-garner-canonical commented Sep 27, 2024

I suspect that this can't be vendored, given the following quote from typing-extensions:

"typing_extensions is treated specially by static type checkers such as mypy and pyright. Objects defined in typing_extensions are treated the same way as equivalent forms in typing."

I haven't used typing_extensions much, but do we have to use its constructs with if TYPE_CHECKING? I see that we have used it this way, for example in ops/pebble.py:

if TYPE_CHECKING:
    from typing_extensions import NotRequired

As far as I can tell, typing_extensions currently supports Python versions 3.8 and higher, so do we really need if TYPE_CHECKING?

Maybe the outcome (with a little bit of further rainy day investigation) will be closing this and making a new issue: let's use Required/NotRequired from typing_extensions to improve our TypeDicts without if TYPE_CHECKING.

@tonyandrewmeyer
Copy link
Contributor

I haven't used typing_extensions much, but do we have to use its constructs with if TYPE_CHECKING?

The advantage of this is that typing_extensions is not a runtime dependency, only a test time one.

@james-garner-canonical
Copy link
Contributor

That makes sense. With from __future__ import annotations we can put the typing_extensions imports behind if TYPE_CHECKING but use its constructs in annotations freely outside if TYPE_CHECKING. Without it, we can still put just the imports inside if TYPE_CHECKING and have the definitions just use string annotations.

Since we're going to go with from __future__ import annotations in future, I think we can close this in favour of (#1336), but I'll hold off on doing so in case anyone wants to investigate vendoring further.

@dimaqq
Copy link
Contributor Author

dimaqq commented Sep 30, 2024

typing_extensions is treated specially by static type checkers such as mypy and pyright. Objects defined in typing_extensions are treated the same way as equivalent forms in typing.

perhaps that means we can't naively vendor some bits from typing_extensions. We could still play with a vendored module that happens to be called "typing_extensions" but I doubt that's worth the hassle, as we'd have to make a funky import to ensure we don't break charms that imports real type_extensions and thus has real module in their virtual env(s).

my take: either real typing_extensions or nothing.

@benhoyt
Copy link
Collaborator

benhoyt commented Oct 1, 2024

The types using NotRequired are _Private in ops/pebble.py. Even if we wanted to make some of them public, we could probably use strings around our type annotations. But we don't think anything is needed here now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
25.04 investigate Needs further investigation - do we want to do this?
Projects
None yet
Development

No branches or pull requests

4 participants