Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mypy reports "Too many positional arguments" for RootModel subclass #7463

Closed
1 task done
Nnonexistent opened this issue Sep 15, 2023 · 5 comments · Fixed by #7677
Closed
1 task done

Mypy reports "Too many positional arguments" for RootModel subclass #7463

Nnonexistent opened this issue Sep 15, 2023 · 5 comments · Fixed by #7677
Assignees
Labels

Comments

@Nnonexistent
Copy link

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

  1. Subclassing the RootModel to use list as a root
  2. Instantiating this new model without keyword root=
    e.g. ListRootModel(['qwe', 'asd']) instead of ListRootModel(root=['qwe', 'asd'])
  3. All is working fine in runtime
  4. Mypy is reporting "Too many positional arguments for ListRootModel"

I understand, that that's a minor issue, but still it would be nice to have a bit cleaner code without compromising mypy analysis

Example Code

from pydantic import RootModel

class ListRootModel(RootModel):
    root: list[str]

ListRootModel(['qwe', 'asd'])  # mypy: Too many positional arguments for "ListRootModel"  [misc]

Python, Pydantic & OS Version

pydantic version: 2.3.0
        pydantic-core version: 2.6.3
          pydantic-core build: profile=release pgo=true
                 install path: /home/nik/.cache/pypoetry/virtualenvs/zia-api-python-PZ2SEBy0-py3.11/lib/python3.11/site-packages/pydantic
               python version: 3.11.3 (main, May 15 2023, 09:03:45) [GCC 11.3.0]
                     platform: Linux-5.15.0-79-generic-x86_64-with-glibc2.35
     optional deps. installed: ['typing-extensions']
@Nnonexistent Nnonexistent added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Sep 15, 2023
@sydney-runkle sydney-runkle self-assigned this Sep 15, 2023
@sydney-runkle
Copy link
Member

Hi,

Thanks for your question. You should be able to use ListRootModel.model_validate(['qwe', 'asd']) without a mypy error.

See the docs on how we recommend using RootModel here.

Let us know if you have any other questions! 😄

@sydney-runkle sydney-runkle added question and removed unconfirmed Bug not yet confirmed as valid/applicable bug V2 Bug related to Pydantic V2 labels Sep 15, 2023
@berzi
Copy link

berzi commented Sep 22, 2023

@sydney-runkle An additional question about this. Let's say I have a root model based on a dict and I want to allow calling it with no arguments so that root gets a value of an empty dict by default, how would I do this? model_validate doesn't allow me to call it with no arguments and it's rather long compared to just MyModel(). Overriding __init__ doesn't seem to change anything: mypy still tells me:

Missing named argument "root" for "MyModel"

@sydney-runkle
Copy link
Member

Hi @berzi,

Thanks for following up - great question! The following works for me without any mypy issues, let me know if you're able to achieve the same.

from typing import Dict

from pydantic import Field, RootModel


class MyModel(RootModel):
    root: Dict[str, str] = Field(default_factory=dict)


m = MyModel()  # no mypy problems

@sydney-runkle
Copy link
Member

Another follow up about the original example - we can do the following with no type errors:

from typing import List

from pydantic import RootModel


ListRootModel = RootModel[List[str]]
ListRootModel(['qwe', 'asd'])

@sydney-runkle
Copy link
Member

@dmontagu, I know you've been working on some mypy fixes/changes, what are your thoughts on this? I'm going to reopen, as the inconsistency with the mypy errors (see the first example) is bugging me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants