Question: Too many arguments for dataclass
See original GitHub issueQuestion
- OS: macOS 10.14
- Python version: 3.7.0
- Pydantic version 0.14
Hi. I decided to try using pydantic instead of JSON Schemas for validating input / output data in my aiohttp web application, and so far run into strange mypy issue. Let consider next code of adding new project,
from aiohttp import web
from pydantic.dataclasses import dataclass
@dataclass
class AddProject:
"""Add new user project."""
name: str
slug: Optional[str]
description: Optional[str]
@login_required
async def add_project(request: web.Request) -> web.Response:
"""Add new user project."""
data = AddProject(**await api_request(request))
user_id = await get_user_id(request)
async with request.app[APP_DB_KEY].acquire() as conn:
project = await create_project(
conn,
user_id=user_id,
name=data.name,
slug=data.slug,
description=data.description)
return api_response(project.dict(), status=201)
It works as expected in runtime, but mypy failed with,
error: Too many arguments for "AddProject"
I tried to satisfy mypy by changing AddProject
instantiation to,
raw_data = await api_request(request)
data = AddProject(
name=raw_data.get('name'),
slug=raw_data.get('slug'),
description=raw_data.get('description'))
but that made mypy failed with,
error: Unexpected keyword argument "name" for "AddProject"
error: Unexpected keyword argument "slug" for "AddProject"
error: Unexpected keyword argument "description" for "AddProject"
I’m a bit curious, as behind that mypy seems to work properly with AddProject
, for example, it knows its attributes and fail in case of using missed ones, like data.does_not_exist
.
Any suggestions on how to make mypy happy? Or this is an expected behaviour, which fixed only by # type: ignore
comment?
ps. api_request
coroutine returns Dict[Any, Any]
if that necessary
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Spurious "too many arguments" with Callables in dataclasses
mypy produces a spurious "too many arguments" error on the following code: from dataclasses import dataclass from typing import Any, Callable ...
Read more >Why does mypy say I have too many arguments - Stack Overflow
When I run mypy against it, it says mypytest.py:4: error: Too many arguments for "Bar". It seems like a bug to me, because...
Read more >python/mypy - Gitter
Unexpected keyword argument “field" for “Dataclass” or error:Too many arguments for “Dataclass” . ... I have a problem whereby mypy does absolutely nothing....
Read more >too-many-arguments / R0913 - Pylint 2.16.0-dev documentation
Description: Used when a function or method takes too many arguments. Created by the design checker.
Read more >too many positional arguments for method call - You.com | The ...
Any subsequent arguments are defined how you want. Usually, you don't need to pass the first self argument because it is passed according...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@samuelcolvin
Unfortunately not. It works well with,
mypy succeed, where previously failed with Too many arguments error.
Enabling
mypy
plugin fixed the issue for me.