question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

get_origin of typing.Iterable returns collections.abc.Iterable

See original GitHub issue
In [1]: import typing_inspect; import typing

In [2]: t = typing.Iterable[int]

In [3]: typing_inspect.get_origin(t)
Out[3]: collections.abc.Iterable

I would expect it to return typing.Iterable instead.

Why? I am using the get_origin and get_args to take apart to walk the type tree, transforming certain nodes and collecting some data (to match a template tree against an actual tree). Similar to something you would do in the AST package or with any other traversal of a nested data structure.

This means I want to be be able to recursively fold over the types, which means I have to be able to deconstruct them and put them back together again. Since collections.abc.Iterable[int] is invalid, this breaks this use case.

This is likely the same issue as https://github.com/ilevkivskyi/typing_inspect/issues/27, but I think I have a slightly different use case. If you have general suggestions on better ways to do this kind of fold, instead of using get_origin and get_args, I am welcome to suggestions.

cc @tonyfast who is also doing some type traversal stuff.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Steworicommented, Jun 10, 2019

I encountered the same issue on supporting Python 3.7 in pytypes. I tried to resolve it by an internal map, e.g. origin_dict[collections.abc.Iterable] == typing.Iterable but discarded that because it only shifted my core issue: I actually needed a way to tell that e.g. List[T] is a subtype of MutableSequence[T] and so on. This became a hassle in the new typing as well (or even worse). So far I came up with this hackish solution: https://github.com/Stewori/pytypes/blob/master/pytypes/type_util.py#L105 but I will probably have to rewrite it again. This is only the first step to make things somehow work again. If it helps you, you can access it as pytypes.type_bases (avaible only in unreleased master branch as of this writing).

0reactions
ilevkivskyicommented, Jun 15, 2019

(I will not have time to work on this any time soon, but PRs are wellcome 😃)

Read more comments on GitHub >

github_iconTop Results From Across the Web

collections.Iterable vs typing.Iterable in type annotation and ...
Iterable can be used in type annotation and checking for whether an object is iterable, i.e., both isinstance(obj, collections. Iterable) and ...
Read more >
typing — Support for type hints — Python 3.11.1 documentation
The function below takes and returns a string and is annotated as follows: ... from collections.abc import Iterable from typing import TypeVar S ......
Read more >
Python typing_inspect.get_origin() Examples
Iterable : collections.abc.Iterable, typing.Sequence: collections.abc.Sequence, typing.Tuple: tuple, }.get(origin, origin). Example #7 ...
Read more >
26.1. typing — Support for type hints - Read the Docs
The function below takes and returns a string and is annotated as follows: ... from typing import Iterable def zero_all_vars(vars: Iterable[LoggedVar[int]]) ...
Read more >
Source code for openmc.material
Iterable import warnings from typing import Optional, ... [docs]class Material(IDManagerMixin): """A material composed of a collection of nuclides/elements.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found