Skip to content

Generic types and isinstance results in Any type parameter #1842

Answered by Daverball
stephenfin asked this question in Q&A
Discussion options

You must be logged in to vote

If you don't specify the type of a type parameter it's always considered to be Any, that's part of the gradual guarantee of Python's type system, setting an upper bound does not change that. If you want the default to be something else you need to use PEP-696 and set a default type for the TypeVar.

The default parameter is new however and only available starting with Python 3.13, in earlier versions you will need to use typing_extensions.TypeVar.

The other way to do it would be to add an annotation to plugin i.e.

import argparse

def load_from_parser(
    namespace: argparse.Namespace, **kwargs: Any
) -> BasePlugin | None:
    if not hasattr(namespace, 'os_auth_type'):
        return None

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@stephenfin
Comment options

@Daverball
Comment options

Answer selected by stephenfin
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