dry-python/classes

mypy report error on simple usage

swuecho opened this issue · 2 comments

from typing import Iterable
from classes import typeclass
from dataclasses import dataclass

@typeclass
def greet(instance) -> str:
    ...

@greet.instance(Iterable, is_protocol=True)
def _greet_str(instance: Iterable) -> str:
    return "Iterable"


@dataclass
class Point(object):
    x:int
    y:int

@greet.instance(Point)
def _greet_point(instance)-> str:
    return f"Point({instance.x}-{instance.y})"

point: Point = Point(1,2)
print(greet(point))
print(greet([1, 2, 3]))

image

image

mypy.ini

[mypy]
ignore_missing_imports = True
allow_redefinition = false
check_untyped_defs = true
ignore_errors = false
implicit_reexport = false
local_partial_types = true
strict_optional = true
strict_equality = true
no_implicit_optional = true
warn_unused_ignores = true
warn_redundant_casts = true
warn_unused_configs = true
warn_unreachable = true
warn_no_return = true


[mypy]
plugins =
  classes.contrib.mypy.classes_plugin

It should be:

@greet.instance(Point)
def _greet_point(instance: Point) -> str:
    return f"Point({instance.x}-{instance.y})"

And this makes it work for me. Make sure you use mypy@0.910

Going to closed as solved, feel free to reopen or ask any other questions.