mypy

Python Type Hints: Specifying a type to be a List of numbers (ints and/or floats)?

∥☆過路亽.° 提交于 2019-12-21 03:14:11
问题 How do I specific a function can take a list of numbers which can be ints or floats? I tried making a new type using Union like so: num = Union[int, float] def quick_sort(arr: List[num]) -> List[num]: ... However, mypy didn't like this: quickSortLomutoFirst.py:32: error: Argument 1 to "quickSortOuter" has incompatible type List[int]; expected List[Union[int, float]] Is there a Type that encompasses ints and floats? 回答1: The short answer to your question is you should use either TypeVars or

Using Mypy local stubs

冷暖自知 提交于 2019-12-14 00:31:38
问题 I am trying the typing hint introduced by Python 3.5 and got a problem by using local stubs as the typing hint with mypy. The experiment I do is to creat kk.py containing def type_check(a): pass Also, I put kk.pyi containing def type_check(a: int):... in the same directory. In this way, I tried to trigger the error of "ncompatible types in assignment" by passing a string to type_check in kk.py. However, when I ran mypy kk.py and get no error. Thus I tried another way that mypy doc suggests,

MyPy gives error “Missing return statement” even when all cases are tested

前提是你 提交于 2019-12-13 14:15:46
问题 I am getting a MyPy error "Missing return statement", even when I check for all possible cases inside a function. For example, in the following code, MyPy is still giving me an error "9: error: Missing return statement" , even though color can only be Color.RED , Color.GREEN , or Color.BLUE , and I test all those cases! class Color(enum.IntEnum): RED: int = 1 GREEN: int = 2 BLUE: int = 3 def test_enum(color: Color) -> str: if color == Color.RED: return "red" elif color == Color.GREEN: return

mypy and attrs: errors typechecking lists of subclasses

假如想象 提交于 2019-12-13 04:58:12
问题 I have a message container that can contain different kinds of messages. For now, there are only text messages. These are my classes: from typing import List, TypeVar import attr @attr.s(auto_attribs=True) class GenericMessage: text: str = attr.ib() GMessage = TypeVar('GMessage', bound=GenericMessage) @attr.s(auto_attribs=True) class TextMessage(GenericMessage): comment: str = attr.ib() @attr.s(auto_attribs=True) class MessageContainer: messages: List[GMessage] = attr.ib() def output_texts

How to use typeshed with mypy?

别等时光非礼了梦想. 提交于 2019-12-12 16:36:53
问题 I cloned typeshed but I can't figure out how to tell mypy to use the type hints it contains, I see no option in mypy --help. The mypy repo does contain reference to the typeshed repo, but pip installing it doesn't not download it. 回答1: Mypy comes bundled with typeshed by default, so you shouldn't need to do anything -- simply doing pip install mypy will install it correctly. Note that typeshed is not a Python module, so it isn't possible to import it or otherwise access it from a Python

mypy: base class has no attribute x, how to type hint in base class

泪湿孤枕 提交于 2019-12-12 10:58:01
问题 I recently discovered mypy and I want my code to be type checked with it. I have a Connector base class: class Connector(): ... some methods, but no __init__ ... And I have several subclasses, they are all connectors, but of different types: class Siphon(Connector) def __init__(): short_name = "S" class Tube(Connector) def __init__(): short_name = "T" When I use these objects, I normally put them in a list: c1 = Siphon() c2 = Tube() list_connectors: List[Connector] = list() list_connectors

How can mypy ignore a single line in a source file?

我们两清 提交于 2019-12-12 09:30:44
问题 I'm using mypy in my python project for type checking. I'm also using PyYAML for reading and writing the project configuration files. Unfortunately, when using the recommended import mechanism from the PyYAML documentation this generates a spurious error in a try/except clause that attempts to import native libraries: from yaml import load, dump try: from yaml import CLoader as Loader, CDumper as Dumper except ImportError: from yaml import Loader, Dumper On my system CLoader and CDumper aren

Pycharm Run External Tool Macro

大兔子大兔子 提交于 2019-12-11 17:54:53
问题 I am trying to run mypy as a "Before Launch" tool in the Run Configuration as a Template. I have the following settings (Program: is the path to the mypy executible). When I run my script I expect the macro to insert the filename of the script as so: mypy.exe myScript.py however it is only running the mypy.exe part (in the console I get an error telling me to supply the target script to the function). I have also tried deleting the working directory field as well, to no avail: (Win10 /

Comparable types with mypy

这一生的挚爱 提交于 2019-12-11 09:18:52
问题 I'm trying to create a generic class to express that a value has lower and upper bounds, and to enforce those bounds. from typing import Any, Optional, TypeVar T = TypeVar("T") class Bounded(object): def __init__(self, minValue: T, maxValue: T) -> None: assert minValue <= maxValue self.__minValue = minValue self.__maxValue = maxValue However, mypy complains that: error: Unsupported left operand type for <= ("T") Apparently typing module doesn't allow me to express this (although it looks like

How to make mypy complain about assigning an Any to an int (part 2)

不羁的心 提交于 2019-12-11 08:13:32
问题 (This is a follow-up to this question.) My code base is fully statically typed (annotation) but at some points there is the Any type, for example because a value was parsed from a JSON string. Here is my minimal example: import json from typing import Any, Dict, Union def main() -> None: data = json.loads('{"value" = "three"}') my_int: int = data['value'] if __name__ == "__main__": main() mypy --strict accepts this code. However I would like to find these places automatically, to take the