type-hinting


Why does ordering matter in Python type hinting?

耗尽温柔 提交于 2020-01-19 17:39:30
问题 Why is this ok class Ship: def __init__(self, parent): self.parent = parent class Fleet: def __init__(self): self.ships = [] def add_ship(self, ship: Ship): self.ships.append(ship) But this is not? class Fleet: def __init__(self): self.ships = [] def add_ship(self, ship: Ship): self.ships.append(ship) class Ship: def __init__(self, parent): self.parent = parent I know that you cannot have circular references in importing. However, this is not an import thing: both of these are in the same

Why does ordering matter in Python type hinting?

◇◆丶佛笑我妖孽 提交于 2020-01-19 17:35:50
问题 Why is this ok class Ship: def __init__(self, parent): self.parent = parent class Fleet: def __init__(self): self.ships = [] def add_ship(self, ship: Ship): self.ships.append(ship) But this is not? class Fleet: def __init__(self): self.ships = [] def add_ship(self, ship: Ship): self.ships.append(ship) class Ship: def __init__(self, parent): self.parent = parent I know that you cannot have circular references in importing. However, this is not an import thing: both of these are in the same

Getting argument type hints of a function in Clojure

青春壹個敷衍的年華 提交于 2020-01-14 13:06:41
问题 I'm looking to extract the type hint information of a function's arguments, but I can't seem to find a way to access that information. For example, say I have the following function: (defn ^Double do-something [^String a, ^String b] 5.0) Pulling the tag is straightforward: (:tag (meta #'do-something)) ; => java.lang.Double For the arguments, however, something like this won't work: (:arglists (meta #'do-something)) ; => ([a b]) This just gives me the arguments and not the type information. Is

What is the correct way to type hint a homogenous Queue in Python3.6 (especially for PyCharm)?

大憨熊 提交于 2020-01-14 08:27:38
问题 I'm writing a fractal generator in Python 3.6, and I use multiprocessing.Queue s to pass messages from the main thread to the workers. This is what I've tried so far, but PyCharm doesn't seem to be able to infer attribute types for items taken from the queues: from typing import NamedTuple, Any, Generic, TypeVar, Tuple from multiprocessing import Process, Queue T = TypeVar() class Message(NamedTuple): method: str id: str data: Any = None class TypedQueue(Generic[T]): def get(self) -> T: ...

Python type hints - specify key, value type for dict subclass

拥有回忆 提交于 2020-01-14 05:16:12
问题 Say I have class A(dict): pass Is there any way to specify the type of the key and values of A ? Can it be done in a way that the types are inherited - so class B(A) would inherit the type values for, say, key and would be able to override the type of value ? Ideally this would be done via the type hints introduced in pep-0484 - but note I am on python 2 so I would need a solution with type comments. If however this is not possible a solution involving metaclasses or any other hack would be

Python type-hinting, indexable object

走远了吗. 提交于 2020-01-13 10:18:31
问题 My function needs to accept an object, from which data can be extracted by index, viz. a List or an instance with defined __getitem__ method. Which type can I use for type hinting this argument? Update: As I understand there are presently no such type, I tried to make one myself: class IndexableContainer(Generic[int, ReturnType]): def __getitem__(self, key: int) -> ReturnType: ... But I get the following error: File "indexable_container.py", line 22, in IndexableContainer class

Type hint for descendant classes

谁说胖子不能爱 提交于 2020-01-11 12:26:53
问题 From the docs page http://php.net/manual/en/language.oop5.typehinting.php If class or interface is specified as type hint then all its children or implementations are allowed too. But: class PimpleChild extends Pimple { //... } interface Pimple_Config { public function configure(Pimple $container); } class PimpleConfigurer_Factories implements Pimple_Config { public function configure(PimpleChild $container) { //... } } returns fatal error. Why? 回答1: If I am not mistaken you get this error:

How should I type-hint an integer variable that can also be infinite?

淺唱寂寞╮ 提交于 2020-01-04 08:23:08
问题 Searching for this topic I came across the following: How to represent integer infinity? I agree with Martijn Peeters that adding a separate special infinity value for int may not be the best of ideas. However, this makes type hinting difficult. Assume the following code: myvar = 10 # type: int myvar = math.inf # <-- raises a typing error because math.inf is a float However, the code behaves everywhere just the way as it should. And my type hinting is correct everywhere else. If I write the

How should I type-hint an integer variable that can also be infinite?

安稳与你 提交于 2020-01-04 08:21:11
问题 Searching for this topic I came across the following: How to represent integer infinity? I agree with Martijn Peeters that adding a separate special infinity value for int may not be the best of ideas. However, this makes type hinting difficult. Assume the following code: myvar = 10 # type: int myvar = math.inf # <-- raises a typing error because math.inf is a float However, the code behaves everywhere just the way as it should. And my type hinting is correct everywhere else. If I write the

typehinting: method should accept any $arg that is an object

眉间皱痕 提交于 2020-01-04 02:36:06
问题 I have a class 'Collection', which has an add method. The add method should only accept objects. So this is the desired behaviour: $x=5;//arbitrary non-object $obj=new Foo; //arbitrary object $collection=new Collection; $collection->add($obj); //should be acceptable arg, no matter the actual class $collection->add($x); //should throw an error because $x is not an object According to the PHP manual, one can typehint methods by prefacing the $arg with a class name. Since all PHP classes are

工具导航Map