Template / Generic user-defined classes in python weird behavior

OrenIshShalom

When I write the following code, mypy issues an error (like it should):

from typing import TypeVar, Generic, List
Data = TypeVar("Data")
class State(Generic[Data]):
    def __init__(self) -> None:
        self.d: Data = 8.9
s = State[int]()

When I remove the (completely redundant, right?) -> None part from the ctor, mypy ignores the type mismatch. Why is that? (note that this is not a duplicate of this)

ethanhs

Mypy only checks functions that are typed. By removing the -> None you are making your __init__ untyped and therefore mypy doesn't check that function.

This is covered in the common issues section of the mypy wiki: https://mypy.readthedocs.io/en/stable/common_issues.html#no-errors-reported-for-obviously-wrong-code

There are several common reasons why obviously wrong code is not flagged as an error.

The function containing the error is not annotated. Functions that do not have any annotations (neither for any argument nor for the return type) are not type-checked, and even the most blatant type errors (e.g. 2 + 'a') pass silently. The solution is to add annotations. Where that isn’t possible, functions without annotations can be checked using --check-untyped-defs.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Default behavior of copy module on user-defined classes

From Dev

Weird behavior with a manually defined strlen

From Dev

Python generics: user defined generic in a callable

From Dev

Python/Bottle template weird behavior when generating html table

From Dev

c++ implicit conversion on user-defined operator for template classes

From Dev

Python datetime weird behavior

From Dev

python thread weird behavior

From Dev

python dictionary weird behavior

From Dev

Set weird behavior (python)

From Dev

Weird scoping behavior in python

From Dev

Ruby conditions and defined? operator weird behavior

From Dev

Weird behavior with operator defined as friend inside class

From Dev

Weird contradictory behavior when casting classes in a interface

From Dev

How to get only user defined classes in Python /PyQt5?

From Dev

How to import and use user defined classes in robot framework with python

From Dev

Weird behavior in Python in array initialization

From Dev

Python, weird memory consumption behavior

From Dev

Weird behavior in python list concatenation

From Dev

The weird behavior of the print function in python

From Dev

Weird python file path behavior

From Dev

Weird sets behavior in python REPL

From Dev

Weird behavior with a lot of numbers python

From Dev

Generic Methods with user defined types

From Dev

Using Template Classes with CUDA keywords in generic classes

From Java

Type hints with user defined classes

From Dev

java User Defined Classes and Objects

From Dev

user defined classes in unordered map

From Dev

Module.ResolveMember behavior for generic classes

From Dev

C++ user-defined string literal template weird issue (getting string literal length as compile-time constant)

Related Related

HotTag

Archive