python-jsonschema / jsonschema

An implementation of the JSON Schema specification for Python
https://python-jsonschema.readthedocs.io
MIT License
4.6k stars 578 forks source link

Error importing jsonschema after release 4.22.0 #1254

Closed miguel-mi-silva closed 5 months ago

miguel-mi-silva commented 5 months ago

If I install jsonschema version 4.22.0 as follows:

poetry add jsonschema@4.22.0 attrs

And run:

import jsonschema

I get the following error:

Traceback (most recent call last):
  File "/home/miguel/repos/proj/t.py", line 11, in <module>
    import jsonschema
  File "/home/miguel/repos/proj/.venv/lib/python3.11/site-packages/jsonschema/__init__.py", line 13, in <module>
    from jsonschema._format import FormatChecker
  File "/home/miguel/repos/proj/.venv/lib/python3.11/site-packages/jsonschema/_format.py", line 11, in <module>
    from jsonschema.exceptions import FormatError
  File "/home/miguel/repos/proj/.venv/lib/python3.11/site-packages/jsonschema/exceptions.py", line 15, in <module>
    from referencing.exceptions import Unresolvable as _Unresolvable
ModuleNotFoundError: No module named 'referencing'

I get not error if I install as follows:

poetry add jsonschema@4.21.2 attrs
Julian commented 5 months ago

I don't use poetry but it certainly should work, I have no issues here. Why are you specifying attrs in your command?

Specifically this works fine here:

⊙  poetry new foo && cd foo && poetry add jsonschema@4.22.0 && poetry run python -c 'import jsonschema; print(jsonschema)'                                                                                                                                                      julian@Airm
Created package foo in foo
Creating virtualenv foo-6IPkgmQ4-py3.12 in /Users/julian/Library/Caches/pypoetry/virtualenvs

Updating dependencies
Resolving dependencies... (0.6s)

Package operations: 5 installs, 0 updates, 0 removals

  - Installing attrs (23.2.0)
  - Installing rpds-py (0.18.1)
  - Installing referencing (0.35.1)
  - Installing jsonschema-specifications (2023.12.1)
  - Installing jsonschema (4.22.0)

Writing lock file
<module 'jsonschema' from '/Users/julian/Library/Caches/pypoetry/virtualenvs/foo-6IPkgmQ4-py3.12/lib/python3.12/site-packages/jsonschema/__init__.py'>

The same of course works with attrs, but there's no reason to be specifying that. Closing but feel free to provide something which reproduces.

evelyn9191 commented 5 months ago

Hi,

I can confirm that I've experienced this as well. I'm using poetry 1.8.2, python 3.12 and updating dependencies:

poetry add jsonschema

This installs the library in version 4.22.0. Then in my python script I call

import jsonschema

def my_function()
    try:
        jsonschema.validate(value, schema)
    except (jsonschema.exceptions.ValidationError, jsonschema.exceptions.SchemaError) as ex:
        pass

And get the same Traceback as @miguel-mi-silva . Pinning jsonschema to <4.22.0 resolves the issue.

Julian commented 5 months ago

Please provide something which reproduces.

felix1m commented 4 months ago

we also have the same problem, poetry 1.7.1 and python 3.10. Upping the version in pyproject.toml from 4.21.1 to 4.22.0 creates the following diff in poetry.lock after poetry lock --no-update: (also tried just poetry lock with similar results)


[[package]]
 name = "jsonschema"
-version = "4.21.1"
-description = "An implementation of JSON Schema validation for Python"
-optional = false
-python-versions = ">=3.8"
-files = [
-    {file = "jsonschema-4.21.1-py3-none-any.whl", hash = "sha256:7996507afae316306f9e2290407761157c6f78002dcf7419acb99822143d1c6f"},
-    {file = "jsonschema-4.21.1.tar.gz", hash = "sha256:85727c00279f5fa6bedbe6238d2aa6403bedd8b4864ab11207d07df3cc1b2ee5"},
-]
-
-[package.dependencies]
-attrs = ">=22.2.0"
-importlib-resources = {version = ">=1.4.0", markers = "python_version < \"3.9\""}
-jsonschema-specifications = ">=2023.03.6"
-pkgutil-resolve-name = {version = ">=1.3.10", markers = "python_version < \"3.9\""}
-referencing = ">=0.28.4"
-rpds-py = ">=0.7.1"
-
-[package.extras]
-format = ["fqdn", "idna", "isoduration", "jsonpointer (>1.13)", "rfc3339-validator", "rfc3987", "uri-template", "webcolors (>=1.11)"]
-format-nongpl = ["fqdn", "idna", "isoduration", "jsonpointer (>1.13)", "rfc3339-validator", "rfc3986-validator (>0.1.0)", "uri-template", "webcolors (>=1.11)"]
-
-[package.source]
-type = "legacy"
-url = "https://pypi.cartwatch.de/simple"
-reference = "signatrix"
-
-[[package]]
-name = "jsonschema-specifications"
-version = "2023.12.1"
-description = "The JSON Schema meta-schemas and vocabularies, exposed as a Registry"
+version = "4.22.0"
+description = ""
 optional = false
-python-versions = ">=3.8"
+python-versions = "*"
 files = [
-    {file = "jsonschema_specifications-2023.12.1-py3-none-any.whl", hash = "sha256:87e4fdf3a94858b8a2ba2778d9ba57d8a9cafca7c7489c46ba0d30a8bc6a9c3c"},
-    {file = "jsonschema_specifications-2023.12.1.tar.gz", hash = "sha256:48a76787b3e70f5ed53f1160d2b81f586e4ca6d1548c5de7085d1682674764cc"},
+    {file = "jsonschema-4.22.0-py3-none-any.whl", hash = "sha256:ff4cfd6b1367a40e7bc6411caec72effadd3db0bbe5017de188f2d6108335802"},
+    {file = "jsonschema-4.22.0.tar.gz", hash = "sha256:5b22d434a45935119af990552c862e5d6d564e8f6601206b305a61fdf661a2b7"},
 ]

-[package.dependencies]
-importlib-resources = {version = ">=1.4.0", markers = "python_version < \"3.9\""}
-referencing = ">=0.31.0"
-
 [package.source]
 type = "legacy"
JAEarly commented 4 months ago

I also had this issue, the suggested solution of pinning jsonschema to <4.22.0 worked for me

Julian commented 4 months ago

There's nothing I can really look into here without some way of reproducing -- it sounds like a poetry issue, especially given that's the situation it sounds like for all of you above? But yeah I can't really look into anything without a way to reproduce. Pinning clearly isn't a real solution to whatever is up.

JAEarly commented 4 months ago

Looks like the underlying issue was with the poetry cache (at least for me). This worked and allowed me to use 4.22.0: poetry cache list poetry cache clear <cache> --all

hadjilucasL commented 3 months ago

The issue seems to happen at least on my side only on a jfrog artifactory (with a remote setup that pulls and caches pypi packages).

Trying this from pypi directly, it works ok.

This is after clearing all local poetry caches.

Example pyproject.toml with pull from jfrog artifactory:

[tool.poetry]
name = "env-temp"
version = "0.1.0"
description = ""
authors = ["HadjilucasL"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.11"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[[tool.poetry.source]]
name = "my-artifactory-internal"
url = "https://mycompany.jfrog.io/artifactory/api/pypi/my-python-virtual/simple"
priority = "primary"

After running two commands to add and upgrade jsonschema

>> poetry add jsonschema==4.21.1

Updating dependencies
Resolving dependencies... 

Package operations: 3 installs, 1 update, 0 removals

  - Installing referencing (0.35.1)
  - Installing jsonschema-specifications (2023.12.1)
  - Installing rpds-py (0.18.1)
  - Downgrading jsonschema (4.22.0 -> 4.21.1)

Writing lock file

>> poetry add jsonschema==4.22.0

Updating dependencies
Resolving dependencies... (0.4s)

Package operations: 0 installs, 1 update, 3 removals

  - Removing jsonschema-specifications (2023.12.1)
  - Removing referencing (0.35.1)
  - Removing rpds-py (0.18.1)
  - Updating jsonschema (4.21.1 -> 4.22.0)

Writing lock file

Will try to purge the remote jfrog cache and try again but weird that this issue happens.

Poetry show is also missing dependency info / python version etc. The hash between the jfrog file and the pypi are the same.

>> poetry show jsonschema       

name         : jsonschema 
version      : 4.22.0     
description  :            

jfrog lock file

[[package]]
name = "jsonschema"
version = "4.22.0"
description = ""
optional = false
python-versions = "*"
files = [
    {file = "jsonschema-4.22.0-py3-none-any.whl", hash = "sha256:ff4cfd6b1367a40e7bc6411caec72effadd3db0bbe5017de188f2d6108335802"},
    {file = "jsonschema-4.22.0.tar.gz", hash = "sha256:5b22d434a45935119af990552c862e5d6d564e8f6601206b305a61fdf661a2b7"},
]

[package.source]
type = "legacy"
url = "https://mycompany.jfrog.io/artifactory/api/pypi/my-python-virtual/simple"
reference = "my-artifactory-internal"

pypi lock file

[[package]]
name = "jsonschema"
version = "4.22.0"
description = "An implementation of JSON Schema validation for Python"
optional = false
python-versions = ">=3.8"
files = [
    {file = "jsonschema-4.22.0-py3-none-any.whl", hash = "sha256:ff4cfd6b1367a40e7bc6411caec72effadd3db0bbe5017de188f2d6108335802"},
    {file = "jsonschema-4.22.0.tar.gz", hash = "sha256:5b22d434a45935119af990552c862e5d6d564e8f6601206b305a61fdf661a2b7"},
]

[package.dependencies]
attrs = ">=22.2.0"
jsonschema-specifications = ">=2023.03.6"
referencing = ">=0.28.4"
rpds-py = ">=0.7.1"

[package.extras]
format = ["fqdn", "idna", "isoduration", "jsonpointer (>1.13)", "rfc3339-validator", "rfc3987", "uri-template", "webcolors (>=1.11)"]
format-nongpl = ["fqdn", "idna", "isoduration", "jsonpointer (>1.13)", "rfc3339-validator", "rfc3986-validator (>0.1.0)", "uri-template", "webcolors (>=1.11)"]
JAEarly commented 3 months ago

Just to add to @hadjilucasL's comment above - I was also running into this issue when using jfrog artifactory, but clearing the caches as per my previous comment worked for me.

Baom92 commented 3 months ago

Hi ! I'm using jsonschema directly in my poetry project and ran in the same issue. Clearing caches doesn't help in resolving the problem. But when deploying my project in Docker, I don't get the same error. Which mean it might be my local environnement causing the issues but I can't locate or resolve the issue !