type-hinting Questions

5

Solved

I'm unsure of the Python convention for type hinting instance variables - I've been doing them within the __init__ constructor arguments like seen here: class LoggedVar(Generic[T]): def __init__(s...

5

Solved

Does the typing module (or any other module) exhibit an API to typecheck a variable at runtime, similar to isinstance() but understanding the type classes defined in typing? I'd like to be to run ...
Decollate asked 27/4, 2017 at 0:48

5

Solved

Why am I receiving this error when I run this code? Traceback (most recent call last): File "main.py", line 13, in <module> def twoSum(self, nums: list[int], target: int) -> ...
Diocese asked 18/8, 2020 at 0:7

2

Solved

I have a function that can accept as input any variable that can be indexed, such as a list or a tuple. How do I indicate this in the type-hint of the function?
Ecesis asked 27/2, 2017 at 12:37

5

Solved

I have a GraphQL schema defined from server and I'd like to write a nice Python GraphQL client for it. I'm looking for a way to transform my GraphQL schema into python classes with type hints such ...
Aplanatic asked 5/11, 2022 at 10:22

7

Solved

I'm trying to write code that validates type hints, and in order to do so I have to find out what kind of object the annotation is. For example, consider this snippet that's supposed to tell the us...
Speakeasy asked 8/3, 2018 at 10:45

4

Solved

Is there a type hint in PHP 8.3 which allows using ExampleClass[] for declaring it is an array of objects of the class ExampleClass? In my specific case, ExampleClass is called Task What I want bu...
Desultory asked 25/2, 2022 at 11:30

3

Solved

I have read that I can reveal the type of variables by using a function called reveal_type, but I can't find how to use it or from where to import it.
Kofu asked 19/6, 2017 at 8:16

5

Solved

Now that Python 3.10 has been released, is there any preference when indicating that a parameter or returned value might be optional, i.e., can be None. So what is preferred: Option 1: def f(parame...
Noami asked 4/10, 2021 at 18:4

6

Solved

I want to annotate a type of a variable in a for-loop. I tried this but it didn't work: for i: int in range(5): pass What I expect is working autocomplete in PyCharm 2016.3.2, but using pre-annot...
Noah asked 13/1, 2017 at 18:47

11

Solved

Is there a way to have Python static analyzers (e.g. in PyCharm, other IDEs) pick up on Typehints on argparse.Namespace objects? Example: parser = argparse.ArgumentParser() parser.add_argument('--...
Ecclesiasticism asked 16/2, 2017 at 16:9

5

Solved

In some (mostly functional) languages you can do something like this: type row = list(datum) or type row = [datum] So that we can build things like this: type row = [datum] type table = [row...
Turbine asked 9/10, 2015 at 18:47

5

Solved

I noticed Python 3.5 and Python 3.6 added a lot of features about static type checking, so I tried with the following code (in python 3.6, stable version). from typing import List a: List[str] = [...
Ebenezer asked 28/12, 2016 at 6:49

5

Solved

Is there a way in php to type hint for two different, unrelated interfaces? For example: interface errorable { function error($msg); } interface recordable { ssh_for_recorder(); } class upload...
Growing asked 1/3, 2011 at 22:51

4

Solved

I'm trying to type hint a numpy ndarray like this: RGB = numpy.dtype[numpy.uint8] ThreeD = tuple[int, int, int] def load_images(paths: list[str]) -> tuple[list[numpy.ndarray[ThreeD, RGB]], list...
Adjective asked 25/8, 2021 at 4:45

3

Solved

What's the difference of using List, Tuple, etc. from typing module: from typing import Tuple def f(points: Tuple): return map(do_stuff, points) As opposed to referring to Python's types direc...
Carinacarinate asked 12/9, 2016 at 20:19

7

Solved

Let's say I have a python function whose single argument is a non-trivial type: from typing import List, Dict ArgType = List[Dict[str, int]] # this could be any non-trivial type def myfun(a: ArgTy...
Penniepenniless asked 3/4, 2019 at 20:21

5

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] ...
Crake asked 25/9, 2018 at 18:8

9

Solved

I have the following code in Python 3: class Position: def __init__(self, x: int, y: int): self.x = x self.y = y def __add__(self, other: Position) -> Position: return Position(self.x + o...
Degrease asked 4/11, 2015 at 22:17

3

Solved

I don't understand the difference when hinting Iterable and Sequence. What is the main difference between those two and when to use which? I think set is an Iterable but not Sequence, are there any...
Counterproposal asked 8/5, 2022 at 0:36

3

Solved

Is there any correct type hint to use for a file or file-like object in Python? For example, how would I type-hint the return value of this function? def foo() -> ???: return open('bar')
Berkow asked 25/7, 2016 at 13:42

2

I'm using Python 3 typing feature for better autocomplete. Many times I have functions that return key/value (dictionary) with specific keys. super simple example: def get_info(name): name_first_l...
Redaredact asked 28/5, 2017 at 9:32

1

Solved

I can't figure how i could type annotate my sqlalchemy models code, what kind of type should i use for my model fields. class Email(Model): __tablename__ = 'emails' name: Column[str] = Column(St...
Herzig asked 12/7, 2022 at 15:37

7

Solved

I am having trouble wrapping my head around the importance of Type hinting in PHP. Apparently 'type hinting' in PHP can be defined as follows: "Type hinting" forces you to only pass objects of...
Cahan asked 11/7, 2016 at 14:4

3

Solved

Assume that I have a function which converts Python data-types to Postgres data-types like this: def map_type(input): if isinstance(input, int): return MyEnum(input) elif isinstance(input, str)...
Plumcot asked 21/9, 2018 at 14:10

© 2022 - 2024 — McMap. All rights reserved.