Skip to content

How to type a config item that can be both a list or dict? #3417

Answered by oprypin
pawamoy asked this question in Q&A
Discussion options

pawamoy
Oct 9, 2023
Maintainer Sponsor

You must be logged in to vote

Type((list, dict)) is a supported way to write this.

def __init__(self, type_: tuple[type[T], ...], length: int | None = None, **kwargs):


Type(list | dict) works by accident only on newer versions of Python at run time

but does not pass mypy:
 error: No overload variant of "Type" matches argument type "UnionType"  [call-overload]
 note: Possible overload variants:
 note:     def [T] __init__(self, type_: type[T], length: int | None = ..., **kwargs: Any) -> Type[T]
 note:     def [T] __init__(self, type_: tuple[type[T], ...], length: int | None = ..., **kwargs: Any) -> Type[T]

Note also that if you find yours…

Replies: 2 comments 1 reply

Comment options

pawamoy
Oct 9, 2023
Maintainer Author Sponsor

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@pawamoy
Comment options

pawamoy Oct 9, 2023
Maintainer Author Sponsor

Answer selected by pawamoy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants