question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Too many arguments for dataclass

See original GitHub issue

Question

  • 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:closed
  • Created 5 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
playpauseandstopcommented, Oct 18, 2018

@samuelcolvin

Unfortunately not. It works well with,

from dataclasses import dataclass as py_dataclass


@py_dataclass
class PyAddProject:

    """Add new user project."""

    name: str
    slug: Optional[str]
    description: Optional[str]

mypy succeed, where previously failed with Too many arguments error.

0reactions
swannknanicommented, Jan 17, 2020

Enabling mypy plugin fixed the issue for me.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found