Skip to content

chore: enable isort#28

Merged
gabe-l-hart merged 8 commits intocontextforge-org:mainfrom
czhao314:isort-fix
Mar 20, 2026
Merged

chore: enable isort#28
gabe-l-hart merged 8 commits intocontextforge-org:mainfrom
czhao314:isort-fix

Conversation

@czhao314
Copy link
Contributor

@czhao314 czhao314 commented Mar 13, 2026

Enable isort for Import Sorting

Summary

Enables isort in pre-commit hooks and applies consistent import sorting across the codebase using the black-compatible profile.

Changes

  • .pre-commit-config.yaml: Enabled isort hook (v6.0.1) with --profile=black
  • pyproject.toml: Added "I" to Ruff lint rules, removed TODO comment
  • Code files: Applied isort formatting to organize imports into standard sections (stdlib → third-party → first-party → local)

Impact

  • 30+ files reformatted with consistent import ordering
  • No functional changes, formatting only
  • Pre-commit hook enforces import sorting going forward
  • All tests pass

Resolves TODO in pyproject.toml line 175.

Address #24

Christina Zhao added 3 commits March 12, 2026 10:31
updated import order, applied isort to adjust the formatting of the import packages for py files
Copy link
Collaborator

@gabe-l-hart gabe-l-hart left a comment

Choose a reason for hiding this comment

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

Thanks for submitting this! This has been a gross corner for a while. I think we need to look carefully at the profile we're using for isort (some of the changes look wrong to me). Additionally, I'm concerned about possible conflicts between ruff and isort which can cause pre-commit to become inoperable (the issue I ran into that caused me to disable this originally).

Comment on lines +166 to +167
# Third Party
# First-Party
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
# Third Party
# First-Party
# First-Party

Copy link
Collaborator

Choose a reason for hiding this comment

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

We still need to remove the duplicate # Third Party heading

cforge/config.py Outdated
# Standard
from contextlib import contextmanager
from functools import lru_cache
import os
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks wrong. isort should keep from ... imports separate from import ... imports.

Copy link
Contributor Author

@czhao314 czhao314 Mar 13, 2026

Choose a reason for hiding this comment

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

**ignore my previous comment, I see what you mean, import os is in the middle of the from imports; for some reason this does pass the isort --check-only command - will take a look

Copy link
Collaborator

Choose a reason for hiding this comment

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

Typically, I've used imports like the following:

from baz import bat
from foo import bar
# ^ alpha sort for all `from ...` imports
import asdf
import qwer
# ^ alpha sort for all `import ...` imports

(but without the editorial comments in between)

Copy link
Contributor Author

@czhao314 czhao314 Mar 13, 2026

Choose a reason for hiding this comment

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

gotcha- i found the issue:
there was one style param force_sort_within_sections = true (sort the imports by module, independent of import style) which was overriding from_first= true which does the sorting you mentioned

I got rid of that, then added force_alphabetical_sort_within_sections = true to sort each by alphabetical order

import json
import tempfile
from pathlib import Path
import tempfile
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here, I think the isort settings must be a bit off from what I'm used to

from pydantic import BaseModel, Field, field_validator, ValidationInfo

# Local
# First-Party
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we want cforge to be Local, not First-Party

Copy link
Contributor Author

Choose a reason for hiding this comment

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

checked and cforge was set as a known first party in the isort config in the toml, I'll adjust it to known local package!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeesh, this is what I get for copying all this config from the upstream project rather than starting from scratch!

pyproject.toml Outdated
# TODO: Enable "I" for import sorting as a separate PR.
select = ["E3", "E4", "E7", "E9", "F", "D1"]
# Also "D1" for docstring present checks and "I" for import sorting.
select = ["E3", "E4", "E7", "E9", "F", "D1", "I"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not clear whether it's this change or the one above to enable the pre-commit hook that actually caused all the changes. I'm also concerned that ruff's I might differ from isort w/ --profile=black.

Here's an example of the isort config that I'd like to use for this project: https://github.com/caikit/caikit/blob/main/.isort.cfg. I think there's a way to add this config directly in pyproject.toml without an independent .isort.cfg file which would be the preferred method.

Copy link
Contributor Author

@czhao314 czhao314 Mar 13, 2026

Choose a reason for hiding this comment

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

it was the change to enable the isort pre-commit hook that caused these changes. i do see ruff's I is causing some sort of issue in the pre-hook.. just tried it again and it fails the black python reformatter and the isort formatting; I'll re-disable it
I think earlier either I might have run the prehook and the "I" didn't save properly so I didn't see any problems, which is completely my bad

Christina Zhao and others added 2 commits March 13, 2026 14:37
updated configs for isort and re-disabled the toml.
Co-authored-by: Gabe Goodhart <ghart@us.ibm.com>
@czhao314 czhao314 requested a review from gabe-l-hart March 17, 2026 17:25
Copy link
Collaborator

@gabe-l-hart gabe-l-hart left a comment

Choose a reason for hiding this comment

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

Two more small style change requests:

  1. Figure out how to remove the extra newline between the from and import lines within a given section
  2. If possible, use case-sensitive alphabetical sorting within sections

There are also a number of local imports in unit tests that show up because the headers for them are changing. This should all get removed, but that can happen in a different PR.

from pathlib import Path
from typing import Optional

import json
Copy link
Collaborator

Choose a reason for hiding this comment

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

There shouldn't be a newline before this. It looks like this change (a newline between the from and import sections) is true in all the files, so probably still some subtle setting to fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed lines_between_types = 1 in the config!

Comment on lines +166 to +167
# Third Party
# First-Party
Copy link
Collaborator

Choose a reason for hiding this comment

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

We still need to remove the duplicate # Third Party heading


def handle_exception(exception: Exception) -> None:
"""Handle an exception and print a friendly error message."""
# Local
Copy link
Collaborator

Choose a reason for hiding this comment

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

NOTE to self: Make sure this is a valid dynamic import to avoid a cycle

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives.ciphers import algorithms, Cipher, modes
Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like this is doing case-insensitive alphabetical sort. In other projects, I've used case-sensitive so all capitalized come first (ALL_CAPS -> CamelCaps -> lower). It's not a setting I have a strong preference about, but I think that's maybe more standard?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds good, changed it to case sensitive (set case_sensitive = true in config)

pyproject.toml Outdated
###############################################################################
known-first-party = ["cforge", "mcpgateway"] # treat "cforge" and "mcpgateway" as FIRSTPARTY
known-local-folder = ["tests", "scripts"] # treat these folders as LOCALFOLDER
known-first-party = ["mcpgateway"] # treat "cforge" and "mcpgateway" as FIRSTPARTY
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
known-first-party = ["mcpgateway"] # treat "cforge" and "mcpgateway" as FIRSTPARTY
known-first-party = ["mcpgateway"] # treat "mcpgateway" as FIRSTPARTY

Christina Zhao added 2 commits March 19, 2026 07:41
removed line between from and import types
changed sorting to case sensitive
just removed the style changes that are no longer applied from the toml file
@czhao314 czhao314 requested a review from gabe-l-hart March 19, 2026 14:45
Copy link
Collaborator

@gabe-l-hart gabe-l-hart left a comment

Choose a reason for hiding this comment

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

One. More. Time! It's all ready to approve, but we need to pull out that spurious PR_ISORT.md file since that should not be committed to the repo.

PR_ISORT.md Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be removed

Branch: isort-fix
AI-usage: none
Signed-off-by: Gabe Goodhart <ghart@us.ibm.com>
Copy link
Collaborator

@gabe-l-hart gabe-l-hart left a comment

Choose a reason for hiding this comment

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

Ship it!

@czhao314
Copy link
Contributor Author

woohoo! also i don't think I have write permissions haha so you'll have to merge it

@gabe-l-hart gabe-l-hart merged commit 30b7497 into contextforge-org:main Mar 20, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants