typing

NoReturn vs. None in “void” functions - type annotations in Python 3.6

拟墨画扇 提交于 2020-05-28 13:36:59
问题 Python 3.6 supports type annotation, like: def foo() -> int: return 42 But what is expected to use when a function hasn't return anything? PEP484 examples mostly use None as a return type, but there is also NoReturn type from typing package. So, the question is what is preferable to use and what is considered a best practice: def foo() -> None: #do smth or from typing import NoReturn def foo() -> NoReturn: #do smth 回答1: NoReturn means the function never returns a value . The function either

Dictionary to dataclasses with inheritance of classes

萝らか妹 提交于 2020-05-17 06:06:21
问题 I have the following class @dataclass_json @dataclass class Input: sources: List[Sources] =None Transformations: List[str] =None As well as: @dataclass_json @dataclass class Source: type: str =None label: str =None path: str = None and the two subclasses: @dataclass_json @dataclass class Csv(Source): csv_path: str=None delimiter: str=';' and @dataclass_json @dataclass class Parquet(Source): parquet_path: str=None Given now the dictionary: parquet={type: 'Parquet', label: 'events', path: '/...

Python: how to override type hint on an instance attribute in a subclass?

不羁岁月 提交于 2020-05-13 06:43:37
问题 Before you dive in, here is my question: how can I use type hints in a subclass to specify a different type on an instance attribute? If you are unclear on what that means, read below, where I have drawn up an example to clarify things. Full Explanation I have an abstract class Foo , and a subclass of Foo called SubclassOfFoo . Foo has an abstract method get_something that returns an object of type Something . Something has a subclass called SubclassOfSomething . SubclassOfSomething has an

Python: how to override type hint on an instance attribute in a subclass?

橙三吉。 提交于 2020-05-13 06:43:05
问题 Before you dive in, here is my question: how can I use type hints in a subclass to specify a different type on an instance attribute? If you are unclear on what that means, read below, where I have drawn up an example to clarify things. Full Explanation I have an abstract class Foo , and a subclass of Foo called SubclassOfFoo . Foo has an abstract method get_something that returns an object of type Something . Something has a subclass called SubclassOfSomething . SubclassOfSomething has an

Type hints: Is it a bad practice to alias primitive data types?

家住魔仙堡 提交于 2020-05-12 19:53:52
问题 In Python documentation for typing & type hints we have the below example: Vector = List[float] def scale(scalar: float, vector: Vector) -> Vector: return [scalar * num for num in vector] Vector type alias clearly shows that type aliases are useful for simplifying complex type signatures. However, what about aliasing primitive data types? Let's contrast two basic examples of function signatures: URL = str def process_url(url: URL) -> URL: pass vs. def process_url(url: str) -> str: pass

Type hints: Is it a bad practice to alias primitive data types?

ぐ巨炮叔叔 提交于 2020-05-12 19:51:54
问题 In Python documentation for typing & type hints we have the below example: Vector = List[float] def scale(scalar: float, vector: Vector) -> Vector: return [scalar * num for num in vector] Vector type alias clearly shows that type aliases are useful for simplifying complex type signatures. However, what about aliasing primitive data types? Let's contrast two basic examples of function signatures: URL = str def process_url(url: URL) -> URL: pass vs. def process_url(url: str) -> str: pass

Type hints: Is it a bad practice to alias primitive data types?

我们两清 提交于 2020-05-12 19:50:07
问题 In Python documentation for typing & type hints we have the below example: Vector = List[float] def scale(scalar: float, vector: Vector) -> Vector: return [scalar * num for num in vector] Vector type alias clearly shows that type aliases are useful for simplifying complex type signatures. However, what about aliasing primitive data types? Let's contrast two basic examples of function signatures: URL = str def process_url(url: URL) -> URL: pass vs. def process_url(url: str) -> str: pass

Python type hint for classes that support __getitem__

给你一囗甜甜゛ 提交于 2020-05-12 15:50:33
问题 I want to add type hints to a function that will accept any object with a __getitem__ method. For instance, in def my_function(hasitems, locator): hasitems[locator] I don't want to restrict hasitems to be a specific type like list or dict . As long as it supports __getitem__ , it's an appropriate argument to my_function . How can I annotate its type without being unnecessarily restrictive? Edit: apparently PyCharm can deduce the appropriate hint in a number of common cases, but not in my

Python type hint for classes that support __getitem__

跟風遠走 提交于 2020-05-12 15:46:43
问题 I want to add type hints to a function that will accept any object with a __getitem__ method. For instance, in def my_function(hasitems, locator): hasitems[locator] I don't want to restrict hasitems to be a specific type like list or dict . As long as it supports __getitem__ , it's an appropriate argument to my_function . How can I annotate its type without being unnecessarily restrictive? Edit: apparently PyCharm can deduce the appropriate hint in a number of common cases, but not in my

C++ template parameter and partial specialization : strong or weak typing?

白昼怎懂夜的黑 提交于 2020-05-11 06:30:58
问题 Today, a friend of mine and I struggled a lot on a stupid mistake, and I make me wondered about how template parameters work in C++. Consider the following code, where I try to partially specialize a class attr<MyClass<I>> where I is an unsigned int , though MyClass expects an int parameter : #include <iostream> template<int I> class MyClass { }; template<typename T> struct attr; template<unsigned int I> struct attr<MyClass<I>> { }; int main(int argc, char *argv[]) { attr<MyClass<1>> att;