typing

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

岁酱吖の 提交于 2020-05-11 06:30:28
问题 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;

Check if a field is typing.Optional

試著忘記壹切 提交于 2020-05-09 05:03:26
问题 What is the best way to check if a field from a class is typing.Optional? Example code: from typing import Optional import re from dataclasses import dataclass, fields @dataclass(frozen=True) class TestClass: required_field_1: str required_field_2: int optional_field: Optional[str] def get_all_optional_fields(fields) -> list: return [field.name for field in fields if __is_optional_field(field)] def __is_optional_field(field) -> bool: regex = '^typing.Union\[.*, NoneType\]$' return re.match

Using typing module in docstring

梦想与她 提交于 2020-04-16 05:09:08
问题 Suppose I have a function with a docstring where I declare the return type as a tuple with two strings: def foo(): """ Returns: Tuple[str, str]: Tuple of first name and last name """ Am I supposed to import Tuple from typing if I don't use it anywhere except for in docstrings? 回答1: PyCharm's docstring support for type hints does not actually use typing . You do not need to import the module. The typing module is only there to support the fact that annotations are executed at runtime; for a

Is there ar preferable way to create type aliases for compound types with Python's typing module?

限于喜欢 提交于 2020-04-11 04:38:10
问题 I have a function with one parameter, which should take an int or a None as argument. There are several ways to create a type alias for such a compound type: # test.py import typing IntOrNone_1 = typing.TypeVar('IntOrNone_1', int, None) IntOrNone_2 = typing.Union[int, None] def my_func1(xyz: IntOrNone_1): return xyz def my_func2(xyz: IntOrNone_2): return xyz my_func1(12) my_func1(None) my_func1(13.7) my_func1('str') my_func2(12) my_func2(None) my_func2(13.7) my_func2('str') Both methods do

How to force static typing in python [duplicate]

旧巷老猫 提交于 2020-04-05 15:06:34
问题 This question already has answers here : How to use type hints in python 3.6? (3 answers) Closed 2 years ago . Since static typing is available in Python 3.6, is it possible to force static typing for a python project or set of python files? 回答1: You can use annotations in Python3, which might help you get some benefits of static typing. However if static typing were to be completely enforced in Python, then it won't be Python anymore. It's a duck-typed dynamic language, and would loose all

How to type node-postgres async query functions in TypeScript?

吃可爱长大的小学妹 提交于 2020-03-25 12:33:25
问题 I am fairly new to TypeScript and started to convert my existing server from ES6 to TypeScript. I am a bit lost and trying to figure out how to declare types for async functions. Here's a stub from the ES6 code: // db.js import { Pool } from 'pg'; const pool = new Pool({ connectionString: 'process.env.DB_CONNECTION', }); export default { query(text, params) { return new Promise((resolve, reject) => { try { const result = pool.query(text, params); return resolve(result); } catch (error) {

Python Typing: Given Set of Values

寵の児 提交于 2020-03-16 08:12:18
问题 I want to type the parameter of a method to be one of a finite set of valid values. So basically, I would like to have the typing equivalent of the following minimal example: valid_parameters = ["value", "other value"] def typed_method(parameter): if not parameter in valid_parameters: raise ValueError("invalid parameter") I checked typing already, but I didn't manage to find a solution. Maybe I was just not able to fully understand the documentation. Is there such a solution? Can it be

How to make Mypy deal with subclasses in functions as expected

泄露秘密 提交于 2020-03-16 06:14:30
问题 I have the following code: from typing import Callable MyCallable = Callable[[object], int] MyCallableSubclass = Callable[['MyObject'], int] def get_id(obj: object) -> int: return id(obj) def get_id_subclass(obj: 'MyObject') -> int: return id(obj) def run_mycallable_function_on_object(obj: object, func: MyCallable) -> int: return func(obj) class MyObject(object): '''Object that is a direct subclass of `object`''' pass my_object = MyObject() # works just fine run_mycallable_function_on_object

How to make Mypy deal with subclasses in functions as expected

点点圈 提交于 2020-03-16 06:12:50
问题 I have the following code: from typing import Callable MyCallable = Callable[[object], int] MyCallableSubclass = Callable[['MyObject'], int] def get_id(obj: object) -> int: return id(obj) def get_id_subclass(obj: 'MyObject') -> int: return id(obj) def run_mycallable_function_on_object(obj: object, func: MyCallable) -> int: return func(obj) class MyObject(object): '''Object that is a direct subclass of `object`''' pass my_object = MyObject() # works just fine run_mycallable_function_on_object

How to make Mypy deal with subclasses in functions as expected

谁说我不能喝 提交于 2020-03-16 06:12:04
问题 I have the following code: from typing import Callable MyCallable = Callable[[object], int] MyCallableSubclass = Callable[['MyObject'], int] def get_id(obj: object) -> int: return id(obj) def get_id_subclass(obj: 'MyObject') -> int: return id(obj) def run_mycallable_function_on_object(obj: object, func: MyCallable) -> int: return func(obj) class MyObject(object): '''Object that is a direct subclass of `object`''' pass my_object = MyObject() # works just fine run_mycallable_function_on_object