Questions tagged [mypy]

Mypy is an optional static type checker for Python.

Mypy is an optional static type checker for Python. Type hints conforming to PEP484 can be added to Python programs using Python's built-in type annotations (since Python 3) or a comment-based annotation syntax for Python 2.

2299 questions
1
vote
1 answer

Inheritable custom class constructor in python

How can I implement a custom constructor (class method) that is inheritable in python? The following minimized example might give an idea: from dataclasses import dataclass from typing import Type, TypeVar T = TypeVar("T") @dataclass class…
Andrew Wagner
  • 22,677
  • 21
  • 86
  • 100
1
vote
1 answer

MyPy's linter showing up error, where's none in logging.disable

MyPy prints out an error "Argument 1 to 'disable' has incompatible type 'str'; expected 'int' According to Python's wiki and running the code, everything is fine, but to MyPy, it's not. Am I doing something wrong? logging.disable('DEBUG')
xrew
  • 13
  • 4
1
vote
1 answer

Creating a typed decorator as classmethod and importing/using it in another file

I am trying to add typing to my project. I have pretty much replicated the pattern for decorator factories. This is the example provided in the linked docs: from typing import Any, Callable, TypeVar F = TypeVar("F", bound=Callable[..., Any]) def…
The Fool
  • 16,715
  • 5
  • 52
  • 86
1
vote
1 answer

Incompatible type with element of Union

I recently received this good answer employed in the following: from typing import Union, cast class Default: """Placeholder for default arguments.""" # ham[] is mutable. `None` has meaning (or is not preferred). def spam(ham:…
1
vote
1 answer

Type-hinting classes belonging to the same interface

Code: import abc class Interface(abc.ABC): @abc.abstractmethod @classmethod def make(cls): ... class AObject(Interface): def __init__(self, a: int): self.a = a @classmethod def make(cls): return…
Levik
  • 13
  • 3
1
vote
1 answer

Pylint incorrectly assumes that type is not subscriptable

I have the following code: config = wandb.config do_something_with_config(config['bar'], config['foo']) For some reason, every time I access a key of config, pylint gives an unsubscriptable-object error. I can add a # pylint:…
Davis Yoshida
  • 1,757
  • 1
  • 10
  • 24
1
vote
1 answer

MyPy Missing return statement

I am having an issue with mypy tossing an error saying I'm missing a return statement. While I have one in the function, it still proceeds to exist. Am I doing something wrong? (I am using python3.8) def misc_menu_choice(misc_menu_input: str) ->…
1
vote
1 answer

type hinting an array

Consider the following minimal example: from array import array def foo(arr: array) -> None: print(arr) I have a function which takes an array argument. My project is statically typed and uses mypy. Mypy complains that: Mypy: Missing type…
Yair M
  • 450
  • 4
  • 12
1
vote
0 answers

Python get type of a field of a class?

Is there a way to get the field type of a class in Python, for use as a type annotation? import dataclasses @dataclasses.dataclass class A: field: str def main() -> None: value: A.field = 123 # Want mypy to flag an error here Where A.field…
edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267
1
vote
1 answer

How to type-annotate a file-like object in Python 3.8+

Previously typing.IO, typing.TextIO, and typing.BinaryIO were available to annotate file-like objects, e.g. those returned by open(). However, after Python 3.8 they seem to be deprecated, and will be removed in Python 3.12. Unlike many of the other…
Migwell
  • 18,631
  • 21
  • 91
  • 160
1
vote
0 answers

how to use TypedDict across files?

I'm a typescript refugee and trying to get python's typechecking to be useful... I have a TypedDict as below: # definition class BaseConfig(TypedDict): debug: bool gcp_project: str table_id: str # example testConfig: BaseConfig = { …
dcsan
  • 11,333
  • 15
  • 77
  • 118
1
vote
1 answer

python/mypy: how to declare ABC using value implemented both as property and attribute

I have an abstract baseclass which uses a value whose implementation in different concrete classes can be either an attribute or a property: from abc import ABC, abstractmethod class Base(ABC): n: int def foo(self): ... …
shaunc
  • 5,317
  • 4
  • 43
  • 58
1
vote
1 answer

Changing dictionary value types in python and Type hinting

What is the most appropriate way to handle typing when modifying the type of values stored in a dictionary? For example, if I run the following code through mypy, it throws an error '... has no attribute "size" [attr-defined]'. from typing import…
1
vote
0 answers

Mypy with telethon

I am building a project based on telethon library. I would like to use the optional typing with mypy, but it fails on all telethon imports error: Skipping analyzing "telethon.tl.types": found module but no type hints or library stubs I can see that…
CucumisSativus
  • 310
  • 3
  • 11
1
vote
2 answers

Unable to specify return type for Django Manager for Custom User Model

I have implemented a custom user model inheriting from AbstractUser: """This module contains models for the users app.""" from django.contrib.auth.models import AbstractUser from django.db import models from django.utils.translation import…
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228