1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
# Tests for marshmallow_dataclass.mypy, using pytest-mypy-plugins
# NOTE: Since pytest-mypy-plugins is not installed on pypy (see setup.py), these tests
# will not even be collected by pytest
- case: NewType_basic
mypy_config: |
follow_imports = silent
plugins = marshmallow_dataclass.mypy
show_error_codes = true
python_version = 3.8
env:
- PYTHONPATH=.
main: |
from dataclasses import dataclass
import marshmallow as ma
from marshmallow_dataclass import NewType
Email = NewType("Email", str, validate=ma.validate.Email)
UserID = NewType("UserID", validate=ma.validate.Length(equal=32), typ=str)
@dataclass
class User:
id: UserID
email: Email
user = User(id="a"*32, email="user@email.com")
reveal_type(user.id) # N: Revealed type is "builtins.str"
reveal_type(user.email) # N: Revealed type is "builtins.str"
User(id=42, email="user@email.com") # E: Argument "id" to "User" has incompatible type "int"; expected "str" [arg-type]
User(id="a"*32, email=["not", "a", "string"]) # E: Argument "email" to "User" has incompatible type "List[str]"; expected "str" [arg-type]
- case: marshmallow_dataclass_keyword_arguments
mypy_config: |
follow_imports = silent
plugins = marshmallow_dataclass.mypy
env:
- PYTHONPATH=.
main: |
from marshmallow_dataclass import dataclass
@dataclass(order=True)
class User:
id: int
name: str
user = User(id=4, name='Johny')
- case: public_custom_types
mypy_config: |
follow_imports = silent
plugins = marshmallow_dataclass.mypy
show_error_codes = true
env:
- PYTHONPATH=.
main: |
from dataclasses import dataclass
import marshmallow
from marshmallow_dataclass.typing import Email, Url
@dataclass
class Website:
url: Url
email: Email
website = Website(url="http://www.example.org", email="admin@example.org")
reveal_type(website.url) # N: Revealed type is "builtins.str"
reveal_type(website.email) # N: Revealed type is "builtins.str"
Website(url=42, email="user@email.com") # E: Argument "url" to "Website" has incompatible type "int"; expected "str" [arg-type]
- case: dataclasses_field_not_a_default
mypy_config: |
follow_imports = silent
plugins = marshmallow_dataclass.mypy
show_error_codes = true
env:
- PYTHONPATH=.
main: |
from dataclasses import field
from marshmallow_dataclass import dataclass
@dataclass
class User:
id: int = field(metadata={"required": True})
name: str
|