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

Add runtime type checking #22

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Add runtime type checking #22

wants to merge 1 commit into from

Conversation

anishathalye
Copy link
Member

@anishathalye anishathalye commented Jan 30, 2025

This patch adds beartype for runtime type checking. This gives us the best of both worlds: we do static type checking of our own library with mypy, and we export our static types, but for clients who do not run static type checking of their own code, runtime type checking in our library can help them catch bugs earlier. tests/test_codex_tool.py::test_bad_argument_type serves as an example: this fails at initialization time of CodexTool, whereas without runtime type checking, this would fail later (e.g., when the user calls the query method on the object).

Because we're performing runtime type checking, some of the imports that were behind if TYPE_CHECKING flags have to be moved to runtime. This patch updates the linter config to allow imports that are only used for type checking.

This patch also switches to consistent from __future__ import annotations everywhere, stops using type hints deprecated by PEP 585 by using beartype.typing instead, and updates the linter config accordingly. This patch also updates the CI config to run static type checking for all supported Python versions.

beartype relies on isinstance for runtime type checks, which needs to be taken into account when using mocks by overriding the __class__ attribute. This patch updates the tests accordingly.

@anishathalye anishathalye requested a review from axl1313 January 30, 2025 23:23
@anishathalye anishathalye removed the request for review from axl1313 January 31, 2025 00:34
@anishathalye anishathalye force-pushed the add-beartype branch 4 times, most recently from ab53920 to 951c5b4 Compare January 31, 2025 01:47
This patch adds beartype for runtime type checking. This gives us the
best of both worlds: we do static type checking of our own library with
mypy, and we export our static types, but for clients who do not run
static type checking of their own code, runtime type checking in our
library can help them catch bugs earlier.
`tests/test_codex_tool.py::test_bad_argument_type` serves as an example:
this fails at initialization time of `CodexTool`, whereas without
runtime type checking, this would fail later (e.g., when the user calls
the `query` method on the object).

Because we're performing runtime type checking, some of the imports that
were behind `if TYPE_CHECKING` flags have to be moved to runtime. This
patch updates the linter config to allow imports that are only used for
type checking.

This patch also switches to consistent `from __future__ import
annotations` everywhere, stops using type hints deprecated by PEP 585 by
using `beartype.typing` instead, and updates the linter config
accordingly. This patch also updates the CI config to run static type
checking for all supported Python versions.

beartype relies on `isinstance` for runtime type checks, which needs to
be taken into account when using mocks by overriding the `__class__`
attribute. This patch updates the tests accordingly.
@anishathalye
Copy link
Member Author

This patch does add some complexity to development. We should consider whether the benefits outweigh the costs. Happy to discuss synchronously if easier.

This patch was motivated by https://github.com/cleanlab/codex/issues/255.

@anishathalye
Copy link
Member Author

Discussed with Angela: plan is to add custom linters (e.g., using semgrep) so developers don't need to remember all the rules introduced in this PR, which seems like it could have been error-prone.

@anishathalye anishathalye removed the request for review from axl1313 February 3, 2025 20:20
@anishathalye anishathalye marked this pull request as draft February 3, 2025 20:20
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