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

Error in mypy plugin reading config from pyproject.toml with mypy >= 0.920 #3579

Closed
3 tasks done
paul-sud opened this issue Dec 26, 2021 · 1 comment
Closed
3 tasks done
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@paul-sud
Copy link

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and still think this is a bug

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.9.0a2
            pydantic compiled: True
                 install path: /Users/paul/.cache/pre-commit/repo8u79aah5/py_env-python3.9/lib/python3.9/site-packages/pydantic
               python version: 3.9.6 (default, Aug 18 2021, 18:03:48)  [Clang 11.0.3 (clang-1103.0.32.62)]
                     platform: macOS-10.16-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']

I'm testing out the new pyproject.toml config support in alpha. I tried to set the config in my pyproject.toml exactly like in the docs: https://pydantic-docs.helpmanual.io/mypy_plugin/#configuring-the-plugin . However, this doesn't seem to work with mypy 0.930, when I run it I get the folllowing traceback:

Error constructing plugin instance of PydanticPlugin

Traceback (most recent call last):
  File "/Users/paul/.cache/pre-commit/repo8u79aah5/py_env-python3.9/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/Users/paul/.cache/pre-commit/repo8u79aah5/py_env-python3.9/lib/python3.9/site-packages/mypy/__main__.py", line 12, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "mypy/main.py", line 96, in main
  File "mypy/main.py", line 173, in run_build
  File "mypy/build.py", line 180, in build
  File "mypy/build.py", line 231, in _build
  File "mypy/build.py", line 478, in load_plugins
  File "mypy/build.py", line 456, in load_plugins_from_config
  File "pydantic/mypy.py", line 91, in pydantic.mypy.PydanticPlugin.__init__
  File "pydantic/mypy.py", line 129, in pydantic.mypy.PydanticPluginConfig.__init__
  File "pydantic/mypy.py", line 130, in pydantic.mypy.PydanticPluginConfig.__init__
  File "/Users/paul/.cache/pre-commit/repo8u79aah5/py_env-python3.9/lib/python3.9/site-packages/tomli/_parser.py", line 56, in load
    s = __fp.read().decode()
AttributeError: 'str' object has no attribute 'decode'

The issue is here: https://github.com/samuelcolvin/pydantic/blob/ef4678999f94625819ebad61b44ea264479aeb0a/pydantic/mypy.py#L129

Newer mypy versions (>=0.920) use tomli for parsing, and tomli's version of load expects the file handle in binary mode explaining the error.

Related:
#2895
#2908

@paul-sud paul-sud added the bug V1 Bug related to Pydantic V1.X label Dec 26, 2021
samuelcolvin added a commit that referenced this issue Dec 29, 2021
@samuelcolvin
Copy link
Member

Thanks for reporting, I've fixed this in #3594 (build currently failing due to issues with pypi).

ntaylorwss pushed a commit to nicejobinc/pydantic that referenced this issue Jun 24, 2022
* cleanup bumping mypy to 0.930, pydantic#3573

* add tests for old mypy

* tweak test-old-mypy job

* alter mypy plugin to work with older versions

* mypy.py compatibility with multiple versions

* fix mypy tests to allow for varied output

* toml parsing, fix pydantic#3579

* formatting :-(

* ignore missing types for toml package

* remove unused ignore_missing_imports

* undo removal of ignore_missing_imports for dotenv

* tweak coverage ignore

* fully uninstall mypy and toml/tomli
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

No branches or pull requests

2 participants