typing

PHP Exception::getCode() contradicts Throwable interface that it implements

浪子不回头ぞ 提交于 2021-02-11 15:37:29
问题 I've found a contradiction I could not understand. Exception::getCode() has this definition: final public Exception::getCode ( void ) : mixed with description: Returns the exception code as integer in Exception but possibly as other type in Exception descendants (for example as string in PDOException) but the Exception class implements Throwable interface that defines: abstract public getCode ( void ) : int So how for an example PDOException as a descendant of Exception could return string

PHP Exception::getCode() contradicts Throwable interface that it implements

僤鯓⒐⒋嵵緔 提交于 2021-02-11 15:36:14
问题 I've found a contradiction I could not understand. Exception::getCode() has this definition: final public Exception::getCode ( void ) : mixed with description: Returns the exception code as integer in Exception but possibly as other type in Exception descendants (for example as string in PDOException) but the Exception class implements Throwable interface that defines: abstract public getCode ( void ) : int So how for an example PDOException as a descendant of Exception could return string

What are built-in Python 3 types that can be compared to each other?

北城以北 提交于 2021-02-10 14:40:59
问题 In Python 2, it was possible to compare objects of different types such as int to str by having an implicit comparison of the text string of types (that is, in lexicographic order, string 'int' is less than string 'str' and string 'list' is less than string 'tuple' ). Hence, in Python 2, 5 < 'hello' returns True . One can read more about why this was allowed in answer to Why is ''>0 True in Python?. In Python 3, this raises builtins.TypeError: unorderable types: int() < str() exception. This

What are built-in Python 3 types that can be compared to each other?

别来无恙 提交于 2021-02-10 14:37:04
问题 In Python 2, it was possible to compare objects of different types such as int to str by having an implicit comparison of the text string of types (that is, in lexicographic order, string 'int' is less than string 'str' and string 'list' is less than string 'tuple' ). Hence, in Python 2, 5 < 'hello' returns True . One can read more about why this was allowed in answer to Why is ''>0 True in Python?. In Python 3, this raises builtins.TypeError: unorderable types: int() < str() exception. This

Exclude object keys by their value type in TypeScript

会有一股神秘感。 提交于 2021-02-08 14:16:01
问题 I want to map an object type to a subtype that includes only keys whose values are of a specific type. For example, something like ExtractNumeric<T> , where ExtractNumeric<{ str: string, num: number }> should be equivalent to the type: { num: number } I've tried this, but it does not work: type ExtractNumeric<T> = { [k in keyof T]: T[k] extends number ? T[k] : never } This snippet throws a type error: let obj: ExtractNumeric<{ str: string, num: number }> = { num: 1 } Because although the str

Exclude object keys by their value type in TypeScript

為{幸葍}努か 提交于 2021-02-08 14:14:45
问题 I want to map an object type to a subtype that includes only keys whose values are of a specific type. For example, something like ExtractNumeric<T> , where ExtractNumeric<{ str: string, num: number }> should be equivalent to the type: { num: number } I've tried this, but it does not work: type ExtractNumeric<T> = { [k in keyof T]: T[k] extends number ? T[k] : never } This snippet throws a type error: let obj: ExtractNumeric<{ str: string, num: number }> = { num: 1 } Because although the str

Exclude object keys by their value type in TypeScript

为君一笑 提交于 2021-02-08 14:14:18
问题 I want to map an object type to a subtype that includes only keys whose values are of a specific type. For example, something like ExtractNumeric<T> , where ExtractNumeric<{ str: string, num: number }> should be equivalent to the type: { num: number } I've tried this, but it does not work: type ExtractNumeric<T> = { [k in keyof T]: T[k] extends number ? T[k] : never } This snippet throws a type error: let obj: ExtractNumeric<{ str: string, num: number }> = { num: 1 } Because although the str

Typing a decorator that curries functions

大憨熊 提交于 2021-01-29 12:29:18
问题 I came across this interesting snippet on GitHub that curries functions and decided to try to add annotations. So far I have the following. from typing import cast, Callable, TypeVar, Any, Union from functools import wraps U = TypeVar('U') def curry(f: Callable[..., U]) -> Callable[..., U]: @wraps(f) def curry_f(*args: Any, **kwargs: Any) -> Union[Callable[..., U], U]: if len(args) + len(kwargs) >= f.__code__.co_argcount: return f(*args, **kwargs) # do I need another @wraps(f) here if curry_f

Which type hint expresses that an attribute must not be None?

不想你离开。 提交于 2021-01-28 06:26:31
问题 In the following code, I need to declare my_attr as anything except None . What should I exchange Any for? from pydantic import BaseModel from typing import Any class MyClass(BaseModel): my_attr: Any 回答1: To achieve this you would need to use a validator, something like: from pydantic import BaseModel, validator class MyClass(BaseModel): my_attr: Any @validator('my_attr', always=True) def check_not_none(cls, value): assert value is not None, 'may not be None' return value But it's unlikely

Typescript: limiting types in object values

一笑奈何 提交于 2021-01-28 06:06:09
问题 I'm trying to create a large object whose values are limited to only 3 types: Texture , Geometry , Script My object would look something like this: var assets: Assets = { sky: <Texture>, ground: <Texture>, city: <Geometry>, people: <Script>, cars: <Script>, sun: <Circle> // <--This should fail because it's not one of the 3 types //... } How can I declare the Assets interface so the value in each key-value pair is limited to these 3 types? I tried starting with: interface Assets{ key: Texture