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

Use of __package__ in pydantic/__init__.py fails if imported from zip #9221

Closed
1 task done
johanandersson-tietoevrycom opened this issue Apr 12, 2024 · 5 comments · Fixed by #9331
Closed
1 task done
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation
Milestone

Comments

@johanandersson-tietoevrycom
Copy link

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

If pydantic folder is placed in zip, then when doing for example 'from pydantic import Field' and __init__.py is run to resolve to 'pydantic.fields' this fails because __package__ is not set. As __package__ is discouraged in docs:
https://docs.python.org/3/reference/import.html, it would be nice if __init__.py used '__spec__.parent' instead as this then works in zip files too. At least do:
if not __package__:
   __package__ = __spec__.parent
at some point before using __package__

Example Code

No response

Python, Pydantic & OS Version

Tested in 2.5.3 -> 2.7.0
            pydantic version: 2.7.0
        pydantic-core version: 2.18.1
          pydantic-core build: profile=release pgo=true
                 install path: C:\Python311\Lib\site-packages\pydantic
               python version: 3.11.7 (tags/v3.11.7:fa7a6f2, Dec  4 2023, 19:24:49) [MSC v.1937 64 bit (AMD64)]
                     platform: Windows-10-10.0.22631-SP0
             related packages: typing_extensions-4.11.0
                       commit: unknown
@johanandersson-tietoevrycom johanandersson-tietoevrycom added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Apr 12, 2024
@samuelcolvin
Copy link
Member

I'm a bit lost, please can you format your issue using markdown so I can read the code.

@johanandersson-tietoevrycom
Copy link
Author

johanandersson-tietoevrycom commented Apr 12, 2024 via email

@sydney-runkle sydney-runkle added this to the 2.7 fixes milestone Apr 14, 2024
@hramezani
Copy link
Member

Thanks @johanandersson-tietoevrycom for reporting this issue.

As package is discouraged in docs: https://docs.python.org/3/reference/import.html,

Agree, it is not recommended to use __package__. Thanks for your suggestion.

Could you please help us to reproduce the problem? We need some steps to reproduce the problem then we can confirm the fix works.

@johanandersson-tietoevrycom
Copy link
Author

@hramezani
Copy link
Member

Thanks @johanandersson-tietoevrycom for helping us to reproduce the problem.

I created #9331 to fix the problem. It would be great if you can confirm the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants