Skip to content

Conversation

kenodegard
Copy link
Contributor

@kenodegard kenodegard commented Mar 25, 2025

Description

While investigating the recent build issues I noticed that we are importing and doing too much in conda/__init__.py, especially with monkey patching the Python stdlib (something we should be moving away from whenever possible).

So here we consolidate the json encoding logic into one place (conda.common.serialize.json). We can mirror this move for the yaml logic in a subsequent PR.

Checklist - did you ...

  • Add a file to the news directory (using the template) for the next release's release notes?
  • Add / update necessary tests?
  • Add / update outdated documentation?

@conda-bot conda-bot added the cla-signed [bot] added once the contributor has signed the CLA label Mar 25, 2025
@github-project-automation github-project-automation bot moved this to 🆕 New in 🔎 Review Mar 25, 2025
elif isinstance(obj, Path):
return str(obj)
return JSONEncoder.default(self, obj)
deprecated.constant("25.9", "26.3", "EntityEncoder", CondaJSONEncoder, addendum="Use `conda.common.serialize.json.CondaJSONEncoder` instead.",)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The title of this PR was misleading. This is not monkeypatching since we are not setting properties on the global json module. It is subclassing and using a standard json module feature.

This does seem like a slow function. Would a faster version keep a mapping of type(obj) -> (needs custom handling) to avoid doing hasattr all the time.

Or we continue to move away from auxlib and don't do any of this.

@kenodegard kenodegard moved this from 🆕 New to 🏗️ In Progress in 🔎 Review May 21, 2025
Copy link

codspeed-hq bot commented May 21, 2025

CodSpeed Instrumentation Performance Report

Merging #14709 will not alter performance

Comparing kenodegard:json-encoder (11b892f) with main (5f78882)

Summary

✅ 21 untouched benchmarks

@jezdez jezdez marked this pull request as ready for review June 26, 2025 13:28
@jezdez jezdez requested a review from a team as a code owner June 26, 2025 13:28
@jezdez
Copy link
Member

jezdez commented Jun 26, 2025

pre-commit.ci autofix

Copy link

codspeed-hq bot commented Jun 26, 2025

CodSpeed WallTime Performance Report

Merging #14709 will not alter performance

Comparing kenodegard:json-encoder (42fe792) with main (21abdfe)

Summary

✅ 21 untouched benchmarks

Co-authored-by: jaimergp <jaimergp@users.noreply.github.com>
@jezdez jezdez requested a review from jaimergp June 27, 2025 09:49
@jezdez
Copy link
Member

jezdez commented Jun 27, 2025

pre-commit.ci autofix

@jezdez jezdez moved this from 🏗️ In Progress to 👀 In Review in 🔎 Review Jun 27, 2025
@@ -239,7 +239,6 @@
from datetime import datetime
from enum import Enum
from functools import reduce
from json import JSONEncoder, dumps as json_dumps, loads as json_loads
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a Python optimization to try to avoid repeated "json.loads" lookups?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, that seems unreasonable, the json module seems pretty straightforward about access to the various module level functions like loads

@@ -15,16 +14,14 @@
from traceback import format_exception, format_exception_only
from typing import TYPE_CHECKING

from requests.exceptions import JSONDecodeError
Copy link
Contributor

@dholth dholth Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#13344

psf/requests#5794

and others...

Maybe the issue of requiring requests' JSONDecodeError is no longer a problem? In case requests is using simplejson apparently.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's a good point, at least the compat code still exists in requests, https://github.com/psf/requests/blob/91a3eabd3dcc4d7f36dd8249e4777a90ef9b4305/src/requests/exceptions.py#L31-L52

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see our new json module here imports the correct compat exception class though: https://github.com/conda/conda/pull/14709/files#diff-84797ed824a58ab550574945be3861f96016e6b0fd2daa654b73925f398b77e2R11-R49

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done something about it in 932b044 (#14709)

@jezdez
Copy link
Member

jezdez commented Jul 1, 2025

pre-commit.ci autofix

@github-project-automation github-project-automation bot moved this from 👀 In Review to ✅ Approved in 🔎 Review Jul 1, 2025
@jezdez jezdez merged commit 01f2286 into conda:main Jul 2, 2025
75 checks passed
@github-project-automation github-project-automation bot moved this from ✅ Approved to 🏁 Done in 🔎 Review Jul 2, 2025
@kenodegard kenodegard deleted the json-encoder branch July 2, 2025 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed [bot] added once the contributor has signed the CLA
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants