typechecking

mypy complains about extended base class' attribute type

mypy complains about extended base class' attribute type Question: I have two base classes A and B that are defined like this: class A(object): def common_function(self): pass class B(object): def __init__(self, a: A): self.a = a def another_common_function(self): pass Class A holds some management information, whereas class B holds some other information, which is based …

Total answers: 2

Check a variable against Union type at runtime in Python 3.6

Check a variable against Union type at runtime in Python 3.6 Question: I’m trying to write a function decorator that uses Python 3.6 type hints to check that a dictionary of arguments respects the type hints and if not raise an error with a clear description of the problem, to be used for HTTP APIs. …

Total answers: 5

Why does defining the argument types for __eq__ throw a MyPy type error?

Why does defining the argument types for __eq__ throw a MyPy type error? Question: I’m using Python 3.5.1 and the newly released MyPy v0.4.1 static type analyzer. I have some more complex code that I’ve reduced down to this simplest possible python class needed to reproduce the error: class MyObject(object): def __init__(self, value: int=5) -> …

Total answers: 3

Self-reference or forward-reference of type annotations in Python

Self-reference or forward-reference of type annotations in Python Question: I’m trying to figure out how self-reference of types work with python3’s type annotations – the docs don’t specify anything regarding this. As an example: from typing import TypeVar, Optional, Generic T = TypeVar(‘T’) class Node(Generic[T]): left = None right = None value = None def …

Total answers: 1

What is the most pythonic way to use len on a scalar?

What is the most pythonic way to use len on a scalar? Question: I read this question python: how to identify if a variable is an array or a scalar but when using the following code I get a false on an np.array as can be demonstrated below. import collections isinstance(np.arange(10), collections.Sequence) # returns false …

Total answers: 4

Use isinstance to test for Unicode string

Use isinstance to test for Unicode string Question: How can I do something like: >>> s = u’hello’ >>> isinstance(s,str) False But I would like isinstance to return True for this Unicode encoded string. Is there a Unicode string object type? Asked By: A.D || Source Answers: Is there a Unicode string object type? Yes, …

Total answers: 3

isinstance(foo,bar) vs type(foo) is bar

isinstance(foo,bar) vs type(foo) is bar Question: A question of semantics, really. Up until recently, if I had to do any typechecking on a structure, I would use type(obj) is list et. al. However since joining SO I’ve noticed everyone (and I mean EVERYONE) uses isinstance(obj,list) instead. It seems they are synonymous, and timeit reveals almost …

Total answers: 5

Python — Check if object is instance of any class from a certain module

Python — Check if object is instance of any class from a certain module Question: Need a way to check if an object is an instance of any class in some particular module. I know I can do it by explicitly importing every class from that module and checking with a tuple: from my_module import …

Total answers: 2

Try/catch or validation for speed?

Try/catch or validation for speed? Question: I’m working with Python and whenever I’ve had to validate function input, I assumed that the input worked, and then caught errors. In my case, I had a universal Vector() class which I used for a few different things, one of which is addition. It functioned both as a …

Total answers: 2

What is the Pythonic Way of Differentiating Between a String and a List?

What is the Pythonic Way of Differentiating Between a String and a List? Question: For my program I have a lot of places where an object can be either a string or a list containing strings and other similar lists. These are generally read from a JSON file. They both need to be treated differently. …

Total answers: 6