Skip to content

Using Type Aliases #132

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

Open
yasserfarouk opened this issue Mar 25, 2020 · 11 comments
Open

Using Type Aliases #132

yasserfarouk opened this issue Mar 25, 2020 · 11 comments

Comments

@yasserfarouk
Copy link

I have a type alias defined using NewType:

OutcomeUtilityMapping = NewType(
    "OutcomeUtilityMapping",
    Union[
        Callable[[Union["Outcome", int, str, float]], "UtilityValue"],
        Mapping[Union[Sequence, Mapping, int, str, float], "UtilityValue"],
    ],
)

Now one of my parameters uses this type:

mapping: OutcomeUtilityMapping

the problem is that the type gets unrolled to something like

mapping (NewType()(OutcomeUtilityMapping, Union[Callable[[Union[ForwardRef, int, str, float]], ForwardRef], Mapping[Union[Sequence, Mapping, int, str, float], ForwardRef]]))

which is unhelpful.

A tip for handling this is to rely on the new annotations using:

from __future__ import annotations
import typing
typing.get_type_hints = lambda obj, *unused: obj

in conf.py but this fails for autodoc-typehints with an error:
AttributeError: '_SpecialForm' object has no attribute 'items'

I know that this should have been resolved in #64 but it seems that I am still having the same issue.

@agronholm
Copy link
Collaborator

What is it that you expect the output to be then?

@arabello
Copy link

Hi,
related question: is there a way to reduce the verbosity of NewType output?

For example: Image instead of NewType()(Image, ndarray)

@agronholm
Copy link
Collaborator

Hi,
related question: is there a way to reduce the verbosity of NewType output?

For example: Image instead of NewType()(Image, ndarray)

What would tell your users that it's a subtype of ndarray then?

@agronholm
Copy link
Collaborator

agronholm commented Jun 21, 2020

@yasserfarouk by the way, you're using NewType incorrectly. The second argument should be an actual class, not an annotation. If you want to make a type alias, just do:

OutcomeUtilityMapping = Union[
    Callable[[Union["Outcome", int, str, float]], "UtilityValue"],
    Mapping[Union[Sequence, Mapping, int, str, float], "UtilityValue"],
]

The reason behind this is that OutcomeUtilityMapping is now a function that will pass the arguments to its second argument which is the Union here, and that won't work. Mypy will give you an error like Argument 2 to NewType(...) must be subclassable (...) when you try something like this.

@krassowski
Copy link

@yasserfarouk you might be interested in this workaround: pandas-dev/pandas#33025 (comment)

@aaugustin
Copy link

aaugustin commented Aug 18, 2021

For example: Image instead of NewType()(Image, ndarray)

I would expect sphinx_autodoc_typehints to render this source:

:obj:`full.path.to.Image`

which would then link to the documentation of the Image type.

With from __future__ import annotations, sphinx.ext.autodoc manages to extract required info, but I'm not sure how exactly.

aaugustin referenced this issue in python-websockets/websockets Aug 28, 2021
Remove sphinx-autodoc-typehints and use native autodoc features
instead, mainly because I can't find a workaround for:
https://github.com/agronholm/sphinx-autodoc-typehints/issues/132

Re-introduce the "common" docs, else linking to send() and recv()
is a mess.
aaugustin referenced this issue in python-websockets/websockets Aug 30, 2021
Remove sphinx-autodoc-typehints and use native autodoc features
instead, mainly because I can't find a workaround for:
https://github.com/agronholm/sphinx-autodoc-typehints/issues/132

Re-introduce the "common" docs, else linking to send() and recv()
is a mess.
aaugustin referenced this issue in python-websockets/websockets Sep 4, 2021
Remove sphinx-autodoc-typehints and use native autodoc features
instead, mainly because I can't find a workaround for:
https://github.com/agronholm/sphinx-autodoc-typehints/issues/132

Re-introduce the "common" docs, else linking to send() and recv()
is a mess.
aaugustin referenced this issue in python-websockets/websockets Sep 5, 2021
Remove sphinx-autodoc-typehints and use native autodoc features
instead, mainly because I can't find a workaround for:
https://github.com/agronholm/sphinx-autodoc-typehints/issues/132

Re-introduce the "common" docs, else linking to send() and recv()
is a mess.
@0x0L
Copy link

0x0L commented Oct 13, 2021

See https://github.com/0x0L/typehints-test

When the sphinx_autodoc_typehints extension is added both the alias and the link are lost

@gaborbernat
Copy link
Member

I don't we can support type aliases in form of simple assignments, as we don't know if something is an alias or not, that information is lost at runtime. The path forward will be those projects to switch to using to explicit type alias via https://www.python.org/dev/peps/pep-0613/ and that would be handled well.

@antonagestam
Copy link

@gaborbernat Is support for TypeAlias tracked somewhere else?

@gaborbernat
Copy link
Member

I don't think so, but PR welcome to add support for it.

@LecrisUT
Copy link

Could this issue be re-opened and tracked as a PEP613 support? #284 is not an ideal workaround because it requires too much manual tracking, and on the other hand, sphinx already supports the PEP613. There are still benefits of having the support here:

  • this one renders it more nicely
  • it seems to have more reliable cross-sphinx linkage
  • neither implementation seem to support cross-sphinx linkage of the TypeAlias (with autodo_type_aliases)

@gaborbernat gaborbernat reopened this Nov 17, 2024
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

No branches or pull requests

9 participants