cattrs icon indicating copy to clipboard operation
cattrs copied to clipboard

Mypy error on structure

Open gleb-chipiga opened this issue 3 years ago • 4 comments

  • cattrs version: 1.1.2
  • Python version: 3.8.6
  • Operating System: Linux x86-64

Description

Run mypy on first example from readme and get error.

What I Did

import cattr
from typing import Tuple

cattr.structure([1.0, 2, "3"], Tuple[int, int, int])

$ mypy example.py
example.py:4: error: Argument 2 has incompatible type "object"; expected "Type[<nothing>]"
Found 1 error in 1 file (checked 1 source file)

gleb-chipiga avatar Jan 08 '21 17:01 gleb-chipiga

Ah, I need to tweak the type annotation (turns out Type[C] basically only works for classes, not abstract types like Tuple or Optional). Will do in the next couple of days.

Tinche avatar Jan 22 '21 14:01 Tinche

Note that I have to change this type annotation to Any, since there's no better way to type check this atm.

Tinche avatar Jan 22 '21 14:01 Tinche

I actually needed to add some overloads. There's no way to correctly check cattr.structure([1.0, 2, "3"], Tuple[int, int, int]) with current Mypy as far as I'm aware. So in those cases you will have to suppress the error in any case.

Tinche avatar Jan 23 '21 00:01 Tinche

We're waiting on this: https://github.com/python/mypy/issues/9773

Tinche avatar Jan 23 '21 00:01 Tinche