Category "python-typing"

How can I check the specific type of TypeVar? [duplicate]

If I have code like T = TypeVar('T') class MyClass(Generic[T]): @classmethod def my_func() -> T: #if T is a string: retur

Python generic that adds typed functionality to extend existing types incorrectly typed

I am interested in creating a generic to add a fixed functionality to different types in python. I have a semi working solution, but it's tying is problematic.

How can I access to __annotations__ of parent class?

Is there any way to access to the typing __annotations __ of the parent class? In the above example, the class Student inherit from class Person, but It does no

How to access the type of an attribute in a TypedDict?

Given a TypedDict, how can you access/use the type of one of its attributes? For example: class Shape(TypedDict): kind: Literal['square', 'circle'] x: int

How to deal with unknown types of functions/methods from imported modules

I'm wondering what's the best approach to deal with unknown types of functions/methods associated with other modules. Note that I'm using strict mode For exampl

Python Typing: Subtype Literal

In Python, I would like to "narrow-down", or "sub-type" a Literal: I would like to have two Literal type aliases, Parent and Child, and I would like to assure t

I'm trying to type annotate around boto3, but module 'botocore.client' has no attribute 'EC2'

I'm writing my own wrapper around boto3 for quick firing functions. I'm trying to type annotate what boto3.session().client('ec2') returns. Debugger says it's &

Make all keys in a typed dict not required

I have an existing TypedDict containing multiple entries: from typing import TypedDict class Params(TypedDict): param1:str param2:str param3:str I

How to annotate that a function produces a dataclass?

Say you want to wrap the dataclass decorator like so: from dataclasses import dataclass def something_else(klass): return klass def my_dataclass(klass):

Python 3 type hinting for decorator

Consider the following code: from typing import Callable, Any TFunc = Callable[..., Any] def get_authenticated_user(): return "John" def require_auth() ->

How to type hint a generator in Python 3?

According to PEP-484, we should be able to type hinting a generator function as follows: from typing import Generator def generate() -> Generator[int, None,

mypy error for function returning TypeVar based on Type[T] argument

I have a function that validates variable based on TypedDict declaration at runtime. I want it to perform validation, raise exception on error and set return ty

IDE deduce Python type

I am writing a method in Python, which looks something like this: def rgb_to_grayscale(image): print(image.shape) pass The one expected type here is nu

Python type hinting with exceptions

I have a function that looks like this: def check_for_errors(result): if 'success' in result: return True if 'error' in result: raise

How do I type hint a method with the type of the enclosing class?

I have the following code in Python 3: class Position: def __init__(self, x: int, y: int): self.x = x self.y = y def __add__(self, oth

python - Difference of typing with AsyncGenerator or AsyncIterator?

I make creating a discord asynchronous library that is fully typed. I have a method that create objects from a aiohttp get requests such as the following exampl