Skip to content

Conversation

@AmberJBlue
Copy link
Member

@AmberJBlue AmberJBlue commented Jul 18, 2025

Restructure code to follow Red Hat AI Python Packaging Best Practices to make the HuggingFace detector package compliant and prepare for publishing guardrails-detectors

Summary by Sourcery

Restructure the detectors project into a compliant Python package layout, adjust import paths, and add packaging metadata and workflows for publishing to PyPI and building container images.

Enhancements:

  • Move source code under src/guardrails_detectors and update import paths accordingly
  • Add pyproject.toml and Hatchling configuration for package building

Build:

  • Configure project metadata, dependencies, and optional extras in pyproject.toml

CI:

  • Update build-and-push workflow to reference new source paths
  • Add GitHub Actions workflow to publish the package to PyPI on release

Documentation:

  • Update README build commands to use new src/guardrails_detectors paths

Tests:

  • Adjust test imports to match the relocated package structure

@sourcery-ai
Copy link

sourcery-ai bot commented Jul 18, 2025

Reviewer's Guide

This PR restructures the repository into a standardized Python package under src/guardrails_detectors, configures packaging with pyproject.toml and hatchling, updates CI workflows for Docker and PyPI publishing, and adjusts imports, documentation, and tests to the new module layout.

Class diagram for updated detector registry structure

classDiagram
    class BaseDetectorRegistry {
        +__init__()
        <<abstract>>
    }
    class RegexDetectorRegistry {
        +__init__()
    }
    class FileTypeDetectorRegistry {
        +__init__()
    }
    BaseDetectorRegistry <|-- RegexDetectorRegistry
    BaseDetectorRegistry <|-- FileTypeDetectorRegistry
Loading

Class diagram for updated FastAPI app structure in built_in

classDiagram
    class FastAPI {
    }
    class Instrumentator {
    }
    class ContentAnalysisHttpRequest {
    }
    class ContentsAnalysisResponse {
    }
    class BaseDetectorRegistry {
    }
    class RegexDetectorRegistry {
    }
    class FileTypeDetectorRegistry {
    }
    FastAPI --> Instrumentator
    FastAPI --> BaseDetectorRegistry
    FastAPI --> RegexDetectorRegistry
    FastAPI --> FileTypeDetectorRegistry
    FastAPI --> ContentAnalysisHttpRequest
    FastAPI --> ContentsAnalysisResponse
Loading

File-Level Changes

Change Details Files
Restructure code into src/guardrails_detectors and update imports
  • Move detectors code into src/guardrails_detectors directory
  • Adjust module imports in application and detector files to new package path
  • Update README build commands to reference src/guardrails_detectors paths
detectors/** → src/guardrails_detectors/**
README.md
Configure Python packaging with pyproject.toml and hatchling
  • Introduce pyproject.toml with project metadata, dependencies, and build settings
  • Define optional and development dependencies and build-system backend
  • Specify hatchling targets for wheel and sdist
pyproject.toml
Enhance CI workflows for Docker builds and PyPI publishing
  • Update build-and-push workflow paths and Dockerfile references to src layout
  • Apply quay.expires-after labels in CI context on moved Dockerfiles
  • Add publish-package.yaml workflow to automate PyPI uploads on release
.github/workflows/build-and-push.yaml
.github/workflows/publish-package.yaml
Adjust tests and pytest configuration to new module paths
  • Update conftest.py to set PYTHONPATH to src/guardrails_detectors
  • Modify test imports to reference guardrails_detectors package
  • Configure pytest.ini options for test discovery under src
tests/**
.tool.pytest.ini_options in pyproject.toml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@AmberJBlue AmberJBlue requested a review from ruivieira July 18, 2025 13:56
@AmberJBlue AmberJBlue marked this pull request as ready for review July 18, 2025 13:56
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @AmberJBlue - I've reviewed your changes - here's some feedback:

  • In tests/conftest.py you reference an undefined src_path when setting up module paths—make sure to define src_path (e.g. as os.path.join(project_root, 'src')) or correct the path variable so imports resolve properly.
  • Update the Docker build context in build-and-push.yaml to match the new package layout (e.g. use src/guardrails_detectors as the build context instead of the old detectors folder) to prevent broken builds.
  • Double-check the package name in your publish-package.yaml (URL and PyPI name) to ensure it matches the hyphenated project name (guardrails-detectors) defined in pyproject.toml.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In tests/conftest.py you reference an undefined `src_path` when setting up module paths—make sure to define `src_path` (e.g. as `os.path.join(project_root, 'src')`) or correct the path variable so imports resolve properly.
- Update the Docker build context in build-and-push.yaml to match the new package layout (e.g. use `src/guardrails_detectors` as the build context instead of the old `detectors` folder) to prevent broken builds.
- Double-check the package name in your publish-package.yaml (URL and PyPI name) to ensure it matches the hyphenated project name (`guardrails-detectors`) defined in pyproject.toml.

## Individual Comments

### Comment 1
<location> `.github/workflows/build-and-push.yaml:86` </location>
<code_context>
+          echo 'LABEL quay.expires-after=7d#' >> src/guardrails_detectors/Dockerfile.builtIn
       - name: Build image
-        run: docker build -t ${{ env.IMAGE_NAME }}:$TAG -f detectors/Dockerfile.hf detectors
+        run: docker build -t ${{ env.IMAGE_NAME }}:$TAG -f src/guardrails_detectors/Dockerfile.hf detectors
       - name: Log in to Quay
         run: docker login -u ${{ secrets.QUAY_ROBOT_USERNAME }} -p ${{ secrets.QUAY_ROBOT_SECRET }} quay.io
</code_context>

<issue_to_address>
Docker build context may be incorrect after directory move.

Update the build context to 'src/guardrails_detectors' to ensure all necessary files are included in the Docker build.
</issue_to_address>

### Comment 2
<location> `.github/workflows/build-and-push.yaml:92` </location>
<code_context>
         run: docker push ${{ env.IMAGE_NAME }}:$TAG
       - name: Build built-in detector image
-        run: docker build -t ${{ env.BUILTIN_IMAGE_NAME }}:$TAG -f detectors/Dockerfile.builtIn detectors
+        run: docker build -t ${{ env.BUILTIN_IMAGE_NAME }}:$TAG -f src/guardrails_detectors/Dockerfile.builtIn detectors
       - name: Push to Quay CI repo
         run: docker push ${{ env.BUILTIN_IMAGE_NAME }}:$TAG
</code_context>

<issue_to_address>
Potential mismatch between Dockerfile location and build context.

If required files are now in 'src/guardrails_detectors', using 'detectors' as the build context may cause build failures or incomplete images.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

echo 'LABEL quay.expires-after=7d#' >> src/guardrails_detectors/Dockerfile.builtIn
- name: Build image
run: docker build -t ${{ env.IMAGE_NAME }}:$TAG -f detectors/Dockerfile.hf detectors
run: docker build -t ${{ env.IMAGE_NAME }}:$TAG -f src/guardrails_detectors/Dockerfile.hf detectors
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): Docker build context may be incorrect after directory move.

Update the build context to 'src/guardrails_detectors' to ensure all necessary files are included in the Docker build.

run: docker push ${{ env.IMAGE_NAME }}:$TAG
- name: Build built-in detector image
run: docker build -t ${{ env.BUILTIN_IMAGE_NAME }}:$TAG -f detectors/Dockerfile.builtIn detectors
run: docker build -t ${{ env.BUILTIN_IMAGE_NAME }}:$TAG -f src/guardrails_detectors/Dockerfile.builtIn detectors
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): Potential mismatch between Dockerfile location and build context.

If required files are now in 'src/guardrails_detectors', using 'detectors' as the build context may cause build failures or incomplete images.

@AmberJBlue AmberJBlue closed this Aug 1, 2025
@AmberJBlue AmberJBlue deleted the hf-detector-package branch August 12, 2025 15:11
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.

1 participant