Skip to content

MyPy supports dataclass transform now! - #352

Merged
jcrist merged 1 commit into
mainfrom
mypy-supports-dataclass-transform-now
Mar 24, 2023
Merged

jcrist merged 1 commit into
mainfrom
mypy-supports-dataclass-transform-now

Conversation

@jcrist

@jcrist jcrist commented Mar 24, 2023

Copy link
Copy Markdown
Member

This landed in mypy version 1.1.1.

We update our type stubs a bit to work better with mypy's implementation of dataclass transform. This shouldn't add new errors when running with older versions of mypy, but does let users using mypy>=1.1.1 catch more errors when working with msgpsec.Struct types (e.g. invalid parameters).

This means the following will now error properly on both pyright (been working for months) and mypy (working since 1.1.1 was released):

import dataclasses
import msgspec


class Point(msgspec.Struct):
    x: int
    y: int


Point(x=1, y="oops")

mypy output

$ mypy temp.py
temp.py:10: error: Argument "y" to "Point" has incompatible type "str"; expected "int"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

pyright output

$ pyright temp.py
No configuration file found.
No pyproject.toml file found.
stubPath /home/jcristharif/Code/msgspec/typings is not a valid directory.
Assuming Python platform Linux
Searching for source files
Found 1 source file
pyright 1.1.290
/home/jcristharif/Code/msgspec/temp.py
  /home/jcristharif/Code/msgspec/temp.py:10:14 - error: Argument of type "Literal['oops']" cannot be assigned to parameter "y" of type "int" in function "__init__"
    "Literal['oops']" is incompatible with "int" (reportGeneralTypeIssues)
1 error, 0 warnings, 0 informations 
Completed in 0.754sec

Since mypy will now catch errors in msgspec.Struct constructors, this also fixes #19.

This landed in `mypy` version 1.1.1.

We update our type stubs a bit to work better with mypy's implementation
of dataclass transform. This shouldn't add new errors when running with
older versions of mypy, but does let users using mypy>=1.1.1 catch more
errors when working with `msgpsec.Struct` types (e.g. invalid parameters)
@jcrist
jcrist merged commit 4f064c8 into main Mar 24, 2023
@jcrist
jcrist deleted the mypy-supports-dataclass-transform-now branch March 24, 2023 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mypy plugin

1 participant