Skip to content

Make typing_extensions a drop in for typing #50

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

Closed
Gobot1234 opened this issue Jun 23, 2022 · 8 comments · Fixed by #196
Closed

Make typing_extensions a drop in for typing #50

Gobot1234 opened this issue Jun 23, 2022 · 8 comments · Fixed by #196
Labels
enhancement New feature or request

Comments

@Gobot1234
Copy link
Contributor

Gobot1234 commented Jun 23, 2022

Currently I find it annoying trying to remember which types from typing_extensions are and aren't in the versions of typing that I have access to. I think this could be solved by making typing_extensions a fully drop in replacement. I think this would also help with some older issues like subclassing Generic with a ParamSpec as a parameter used to be.

#50 (comment)

@srittau
Copy link
Collaborator

srittau commented Jun 27, 2022

I would be fine with this, but I am mostly indifferent to it.

@vwxyzjn
Copy link

vwxyzjn commented Jun 28, 2022

Yeah, this is very inconvenient. I tried using it as a drop-in replacement because we have dozens of files, but it doesn't work:

from typing_extensions import Literal, Mapping, Optional, Tuple, Union

yields

ImportError: cannot import name 'Mapping' from 'typing_extensions'

@vwxyzjn
Copy link

vwxyzjn commented Jun 28, 2022

Effectively we needed to do things like

from typing import Literal, Mapping, Optional, Tuple, Union
try:
    import Literal
except ImportError:
    from typing extensions import Literal

which is a labor-intensive process and not that maintainable.

@JelleZijlstra
Copy link
Member

I'm sympathetic to this idea; it's hard to remember what you need to import from typing-extensions depending on the Python version. At runtime we can simply add from typing import * to typing_extensions.py. The stubs for typing-extensions can work similarly. Some type checkers may also need minor changes.

For reference, these are the names in typing.__all__ but not typing_extensions.__all__.

{'AbstractSet',
 'Any',
 'AnyStr',
 'BinaryIO',
 'ByteString',
 'Callable',
 'Collection',
 'Container',
 'Dict',
 'ForwardRef',
 'FrozenSet',
 'Generator',
 'Generic',
 'Hashable',
 'IO',
 'ItemsView',
 'Iterable',
 'Iterator',
 'KeysView',
 'List',
 'Mapping',
 'MappingView',
 'Match',
 'MutableMapping',
 'MutableSequence',
 'MutableSet',
 'NamedTuple',
 'Optional',
 'Pattern',
 'Reversible',
 'Sequence',
 'Set',
 'Sized',
 'SupportsAbs',
 'SupportsBytes',
 'SupportsComplex',
 'SupportsFloat',
 'SupportsInt',
 'SupportsRound',
 'TextIO',
 'Tuple',
 'TypeVar',
 'Union',
 'ValuesView',
 'cast',
 'no_type_check',
 'no_type_check_decorator'}

@AlexWaygood
Copy link
Member

I also quite like this idea (though I don't feel particularly strongly about it, like @srittau). @JelleZijlstra's suggested implementation sounds good to me.

@vwxyzjn
Copy link

vwxyzjn commented Jun 28, 2022

Thanks @JelleZijlstra! It would really help with our use case.

As a side note, we tried to patch it somehow:

if sys.version_info < (3, 8):
    from typing_extensions import get_origin as get_origin_typing_extensions
    def get_origin_wrapped(t):
        if sys.version_info < (3, 8):
            return get_origin_typing_extensions(t)
        return typing.get_origin(t)
    setattr(typing, "Literal", typing_extensions.Literal)
    setattr(typing, "get_origin", get_origin_wrapped)

@julien-blanchon
Copy link

Yes, please it's a huge pain to use both typing and typing_extensions depending on the method

JelleZijlstra added a commit to JelleZijlstra/typing_extensions that referenced this issue May 25, 2023
Fixes python#50.

I decided to exclude top-level names that have a definite removal
timeline (io, re, ByteString); there's no point in re-exporting them
now.

Many other names are deprecated without a definite removal plan
(PEP 585). If they do get removed eventually, typing-extensions can
simply re-export the builtin/collections.abc names.

In the documentation, I add a separate section listing all the names
that are pure aliases for typing. This makes it easier to distinguish
the cases where there is something more interesting going on in
typing-extensions.
@JelleZijlstra
Copy link
Member

PR in #196. I didn't do the import * approach I suggested above; it's better to be explicit about this.

@AlexWaygood AlexWaygood added the enhancement New feature or request label Jun 6, 2023
JelleZijlstra added a commit that referenced this issue Jun 9, 2023
* Re-export all names from typing

Fixes #50.

I decided to exclude top-level names that have a definite removal
timeline (io, re, ByteString); there's no point in re-exporting them
now.

Many other names are deprecated without a definite removal plan
(PEP 585). If they do get removed eventually, typing-extensions can
simply re-export the builtin/collections.abc names.

In the documentation, I add a separate section listing all the names
that are pure aliases for typing. This makes it easier to distinguish
the cases where there is something more interesting going on in
typing-extensions.

Co-authored-by: Alex Waygood <[email protected]>
Co-authored-by: Shantanu <[email protected]>
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 a pull request may close this issue.

6 participants