Skip to main content

All Questions

Tagged with
3 votes
1 answer
415 views

Why does mypy fail to detect that a raw tuple value is a NamedTuple?

I have this piece of code: from typing import NamedTuple class KeyTuple(NamedTuple): key: str storage: dict[KeyTuple, int] = {} storage.get(("kek",)) that raises an error: test_mypy....
Leydenberg's user avatar
0 votes
1 answer
231 views

Mypy does not detect errors regarding the use of NamedTuple inside functions

In the following code the defintions of x2 and x3 are obviously wrong, yet mypy does not complain. from typing import NamedTuple class X(NamedTuple): a: float b: float c: float def foo():...
Friedrich Gretz's user avatar
0 votes
0 answers
515 views

NamedTuple indexing not supported by mypy?

I define a NamedTuple as follow to get data scraped from a webpage (grades from a restaurant rating website): class Grades(NamedTuple): Value: Optional[int] = None Price: Optional[int] = None ...
user2969402's user avatar
  • 1,241
1 vote
1 answer
584 views

typechecking python NamedTuple in both python 2 and 3

I have a Python code that works with both python2 and python3 and uses mypy. I managed to have my namedtuples typechecked with the following really convoluted method: try: from typing import ...
Piotr Lesnicki's user avatar
2 votes
2 answers
1k views

How both ignore type check and obey line <80 chars

I have this data type that just groups related data. It should be a struct-like thing, so I opted for a namedtuple. ConfigOption = namedtuple('ConfigOption', 'one two animal vehicle fairytale') On ...
xtofl's user avatar
  • 41.3k
52 votes
6 answers
29k views

What are the main differences of NamedTuple and TypedDict in Python / mypy

It seems to me that NamedTuple and TypedDict are fairly similar and the Python developers themselves recognized that. Concerning the PEP, I would rather add a common section about NamedTuple and ...
Christoph's user avatar
  • 27.5k
1 vote
1 answer
1k views

How to ignore mypy errors when using NamedTuples with default values

I have been able to have mypy do a type check on NamedTuple and use default values for NamedTuple. However, I always have an error raised by mypy when I use default value. Here is my code (I use ...
Jean-Francois T.'s user avatar
1 vote
0 answers
880 views

typing.NamedTuple, abc.NamedTuple mixins in python 3.6.2?

I am new to python (3.6.2 is the version I am using). I am currently porting (trying to port) a DSL implementation from Scala to python for some projet in which python is imposed. So I am looking ...
remi's user avatar
  • 566
34 votes
2 answers
13k views

A way to subclass NamedTuple for purposes of typechecking

I have several namedtuples that share some fields. I have a function that accepts these tuples and is guaranteed to only interact with the shared fields. I want to typecheck such code in mypy. An ...
wuzwm's user avatar
  • 531
1 vote
1 answer
1k views

How do you implement type-checking on methods for classes that inherit from NamedTuple in mypy?

According to the mypy docs, if a class needs to reference itself, it can use a forward-reference. This seems to work fine for normal classes but I'm having trouble getting it to work with classes that ...
Stephan Fitzpatrick's user avatar