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

Integrate and Enhance Type Checking with Mypy #264

Merged
merged 11 commits into from Dec 12, 2023
Merged

Integrate and Enhance Type Checking with Mypy #264

merged 11 commits into from Dec 12, 2023

Conversation

gao-hongnan
Copy link
Contributor

@gao-hongnan gao-hongnan commented Dec 9, 2023

This PR introduces several enhancements and changes focusing on integrating and utilizing mypy for improved type checking and code quality. Below are the key points of this PR:

Mypy Configuration File: Closely aligned with the OpenAI Python mypy settings.

Pin Mypy Versions: The versions of mypy are pinned.

Mypy Pre-commit Implementation: Integrated mypy into the pre-commit hooks.

Mypy GitHub Actions: Implemented GitHub Actions for running mypy checks.

Mypy Artifacts Logs for Persistency: Configured the workflow to generate and store mypy logs as artifacts. This provides a persistent record of the type checks for each build, useful for debugging and historical reference.

Fixed Mypy Errors in cli/cli.py and cli/usage.py: As a proof of concept (POC), type errors in cli/cli.py and cli/usage.py have been fixed. Other errors can be fixed gradually as a collective effort.

Some screenshots.
Screenshot 2023-12-09 at 11 08 39 PM

Screenshot 2023-12-09 at 11 09 11 PM

As future enhancement, we can group ruff, mypy, test etc under a single workflow in github actions.

Summary by CodeRabbit

  • New Features

    • Added a new continuous integration workflow to enhance code quality and reliability.
    • Added a type alias ModelNames for improved type checking and clarity in code.
    • Implemented new static type checks using mypy for specific Python files.
  • Documentation

    • Updated type hints and return types in various functions for better code documentation and type safety.
  • Refactor

    • Removed the ruff-format hook from the pre-commit configuration.
    • Enhanced import statements with additional type hints.
  • Dependencies

    • Added mypy version 1.7.1 to dependencies for static type checking.
    • Included typer in the list of dependencies for creating command-line interfaces.
    • Removed ruff version 0.1.7 from dependencies.
  • Bug Fixes

    • Adjusted mypy configuration to enforce stricter type checking and resolve potential type-related issues.

Copy link
Contributor

coderabbitai bot commented Dec 9, 2023

Walkthrough

The project has introduced a new continuous integration workflow, enhanced type checking with mypy, and updated the pre-commit configuration. Type aliases and hints have been refined across various Python files, and dependency changes reflect a shift towards mypy and typer, moving away from ruff.

Changes

File Summary
.github/workflows/mypy.yml
.pre-commit-config.yaml
Added a new CI workflow for mypy checks. Updated pre-commit hooks, removed ruff-format, and added ci_type_mypy.
.mypy.ini Enhanced mypy configuration for stricter type checking.
instructor/_types/_alias.py Introduced ModelNames type alias using typing_extensions.
instructor/cli/usage.py Updated type hints, added import for ModelNames, and modified function return and parameter types.
instructor/sandbox.py Added new variable assignments and comparison.
requirements.txt Updated dependencies: added mypy and typer, removed ruff.

🐇✨
In the burrow of the code, a change does bloom,
With mypy in tow, no type errors loom.
CI workflows dance, and pre-commits sing,
A type-safe haven, the rabbit's new spring.
🌱🎉

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on X ?


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • You can reply to a review comment from CodeRabbit.
  • You can also chat with CodeRabbit bot directly around the specific lines of code or files in the PR by tagging @coderabbitai in a new comment.
  • You can tag @coderabbitai in a PR comment and ask questions about the PR and the codebase.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 5588786 and ecae75e.
Files ignored due to filter (2)
  • poetry.lock
  • pyproject.toml
Files selected for processing (7)
  • .github/workflows/mypy.yml (1 hunks)
  • .mypy.ini (1 hunks)
  • .pre-commit-config.yaml (1 hunks)
  • instructor/_types/_alias.py (1 hunks)
  • instructor/cli/usage.py (7 hunks)
  • instructor/sandbox.py (1 hunks)
  • requirements.txt (1 hunks)
Files skipped from review due to trivial changes (1)
  • instructor/_types/_alias.py
Additional comments: 3
.mypy.ini (1)
  • 1-29: The .mypy.ini configuration aligns with the PR's objectives to enhance type checking capabilities and code quality. The settings are strict and closely follow the standards used by OpenAI's Python projects, as mentioned in the PR objectives. The references to other projects' mypy configurations are a good practice for maintaining consistency and standards.
instructor/sandbox.py (1)
  • 1-3: The code appears to be a simple comparison operation without any context of usage or integration with the rest of the system. Without additional context, it's difficult to assess the relevance of this change to the PR's objectives. However, the code itself is correct and does not exhibit any issues.
requirements.txt (1)
  • 6-9: The changes to requirements.txt align with the PR's objective to integrate mypy for static type checking and improve code quality. The addition of mypy==1.7.1 and typer, and the removal of ruff==0.1.7 are consistent with the stated goals.

Comment on lines +11 to +23
hooks:
- id: ci_type_mypy
name: Static Type Check
entry: >
bash -c 'set -o pipefail;
export CUSTOM_PACKAGES="instructor/cli/cli.py instructor/cli/usage.py" &&
export CUSTOM_FLAGS="--python-version=3.9 --color-output --no-pretty" &&
curl -sSL https://raw.githubusercontent.com/gao-hongnan/omniverse/continuous-integration/scripts/devops/continuous-integration/type_mypy.sh |
bash'
language: system
types: [python]
pass_filenames: false
Copy link
Contributor

Choose a reason for hiding this comment

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

The new ci_type_mypy hook is correctly configured to perform static type checks using mypy. The entry script is fetched from a remote repository, which could be a potential security risk if the script content changes. Consider pinning the script to a specific commit hash to mitigate this risk.

.github/workflows/mypy.yml Show resolved Hide resolved
Copy link
Owner

@jxnl jxnl left a comment

Choose a reason for hiding this comment

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

Looks good. Are you planning to add more before merge?

…from continuous-integration to MyPy for better readability.
@gao-hongnan
Copy link
Contributor Author

Looks good. Are you planning to add more before merge?

If this PR looks decent to you, I can help set up a more complete suite of pre-merge continuous integration (CI) checks.

@jxnl
Copy link
Owner

jxnl commented Dec 10, 2023

would love that!

@gao-hongnan
Copy link
Contributor Author

gao-hongnan commented Dec 11, 2023

would love that!

Got it, I will open a new PR for that. For now, I am finding out why the latest test/coverage check is unsuccessful on this branch. Apologies, accidentally clicked on the request review button again.

@gao-hongnan
Copy link
Contributor Author

gao-hongnan commented Dec 11, 2023

Hi @jxnl, after inspecting the errors arising from the pre-merge test, it seems that all failed cases have exactly the same error:

openai.APIConnectionError: Connection error.

which may be related to the secrets OPENAI_API_KEY defined in the github actions.

In light of this error, it could be because GitHub Actions provides restricted access to secrets when running workflows triggered by pull requests from forks. This is further confirmed after I checked stackoverflow and a blog post from GitHub. The blog post mentioned it can be somewhat (unsure) resolved by adding pull_request_target to the workflow.

@jxnl
Copy link
Owner

jxnl commented Dec 11, 2023

that makes sense running tests now

@gao-hongnan
Copy link
Contributor Author

gao-hongnan commented Dec 12, 2023

that makes sense running tests now

You might have to first define the following in .github/workflows/test.yml:

pull_request_target:
  branches:
    - main

and make a push to the main branch first.

But after reading a bunch of articles, there are still potential risks involved, see here. As of now I do not have a better alternative given that the recent changes also depends on ${{ secrets.OPENAI_API_KEY }} when running certain tests.

Copy link
Owner

@jxnl jxnl left a comment

Choose a reason for hiding this comment

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

+1

@jxnl jxnl merged commit 94e3296 into jxnl:main Dec 12, 2023
5 of 6 checks passed
gao-hongnan added a commit to gao-hongnan/instructor that referenced this pull request Dec 21, 2023
…ion to mypy to be in sync with the naming convention of other workflows jxnl#264.
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.

None yet

2 participants