python-typing

How can I keep imports lightweight and still properly type annotate?

∥☆過路亽.° 提交于 2021-02-11 06:14:38
问题 Tensorflow is a super heavy import. I want to import it only when it's needed. However, I have a model loading function like this: from typing import Dict, Any from keras.models import Model # Heavy import! Takes 2 seconds or so! # Model loading is a heavy task. Only do it once and keep it in memory model = None # type: Optional[Model] def load_model(config: Dict[str, Any], shape) -> Model: """Load a model.""" if globals()['model'] is None: globals()['model'] = create_model(wili.n_classes,

How can I keep imports lightweight and still properly type annotate?

家住魔仙堡 提交于 2021-02-11 06:13:12
问题 Tensorflow is a super heavy import. I want to import it only when it's needed. However, I have a model loading function like this: from typing import Dict, Any from keras.models import Model # Heavy import! Takes 2 seconds or so! # Model loading is a heavy task. Only do it once and keep it in memory model = None # type: Optional[Model] def load_model(config: Dict[str, Any], shape) -> Model: """Load a model.""" if globals()['model'] is None: globals()['model'] = create_model(wili.n_classes,

How to statically get TypeVar parameters from a Generic for use in static type checking?

我与影子孤独终老i 提交于 2021-02-08 10:40:36
问题 I have a class that inherits from typing.Generic and passes in one TypeVar as the parameter. Later on in the code, I would like to: Statically (not at runtime) get the TypeVar parameter out from the class Alias it to another type variable Use that alias to type hint the return of a function Is there some way in Python that I can make this happen? The only piece I am missing is step 1, how to get the type parameter out of a type variable My Use Case from abc import ABC, abstractmethod from

What type in the typing module describes a class? What type describes a function?

前提是你 提交于 2021-02-07 19:16:06
问题 The new typing module in Python 3.5 provides a number of tools for use in type annotations. Does it provide an object or type that encapsulates the idea of class ? How about the idea of function ? In the following code, which defines a decorator, what should stand in for class_ ? What should stand in for function ? ( typing.Callable is inadequate, because for example a class is callable, but the code is trying to identify methods.) (The no_type_check() decorator in the typing module itself

What type in the typing module describes a class? What type describes a function?

好久不见. 提交于 2021-02-07 19:15:15
问题 The new typing module in Python 3.5 provides a number of tools for use in type annotations. Does it provide an object or type that encapsulates the idea of class ? How about the idea of function ? In the following code, which defines a decorator, what should stand in for class_ ? What should stand in for function ? ( typing.Callable is inadequate, because for example a class is callable, but the code is trying to identify methods.) (The no_type_check() decorator in the typing module itself

How to combine a custom protocol with the Callable protocol?

感情迁移 提交于 2021-02-07 12:06:17
问题 I have a decorator that takes a function and returns the same function with some added attributes: import functools from typing import * def decorator(func: Callable) -> Callable: func.attr1 = "spam" func.attr2 = "eggs" return func How do I type hint the return value of decorator ? I want the type hint to convey two pieces of information: the return value is a Callable the return value has attributes attr1 and attr2 If I write a protocol, class CallableWithAttrs(Protocol): attr1: str attr2:

How to combine a custom protocol with the Callable protocol?

穿精又带淫゛_ 提交于 2021-02-07 12:04:42
问题 I have a decorator that takes a function and returns the same function with some added attributes: import functools from typing import * def decorator(func: Callable) -> Callable: func.attr1 = "spam" func.attr2 = "eggs" return func How do I type hint the return value of decorator ? I want the type hint to convey two pieces of information: the return value is a Callable the return value has attributes attr1 and attr2 If I write a protocol, class CallableWithAttrs(Protocol): attr1: str attr2:

How can I make a recursive Python type defined over several aliases?

て烟熏妆下的殇ゞ 提交于 2021-01-22 19:16:44
问题 I want this logical type structure: ObjectType = Dict[str, 'EntryType'] ListType = List['EntryType'] EntryType = Union[str, 'ListType', 'ObjectType'] mypy reports these errors: mdl/structure.py:7: error: Cannot resolve name "ObjectType" (possible cyclic definition) mdl/structure.py:7: error: Cannot resolve name "EntryType" (possible cyclic definition) mdl/structure.py:8: error: Cannot resolve name "ListType" (possible cyclic definition) ... Is there some way to encode this recursive data type

How can I make a recursive Python type defined over several aliases?

ぐ巨炮叔叔 提交于 2021-01-22 19:13:55
问题 I want this logical type structure: ObjectType = Dict[str, 'EntryType'] ListType = List['EntryType'] EntryType = Union[str, 'ListType', 'ObjectType'] mypy reports these errors: mdl/structure.py:7: error: Cannot resolve name "ObjectType" (possible cyclic definition) mdl/structure.py:7: error: Cannot resolve name "EntryType" (possible cyclic definition) mdl/structure.py:8: error: Cannot resolve name "ListType" (possible cyclic definition) ... Is there some way to encode this recursive data type

How do I annotate a callable with *args and **kwargs?

南笙酒味 提交于 2021-01-19 06:22:52
问题 I have a function which returns a function. I would like to find a proper type annotation. However, the returned function has *args and *kwargs . How is that annotated within Callable[[Parameters???], ReturnType] ? Example: from typing import Callable import io import pandas as pd def get_conversion_function(file_type: str) -> Callable[[io.BytesIO, TODO], pd.DataFrame]: def to_csv(bytes_, *args, **kwargs): return pd.read_csv(bytes_, **kwargs) if file_type == "csv": return to_csv 回答1: As I