Skip to content

chore: Enhance type checking with mypy and improve code quality #285

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

Merged
merged 3 commits into from
Jul 16, 2024

Conversation

cdragos
Copy link
Contributor

@cdragos cdragos commented Jul 12, 2024

This PR enhances our code quality checks and type safety:

  1. Integrates mypy into our check_code_quality process
  2. Improves path handling using pathlib.Path for better cross-platform support
  3. Fixes several type-related issues caught by mypy
  4. Updates .dockerignore

@cdragos cdragos force-pushed the chore/pyright branch 3 times, most recently from 2c93bd9 to 09eb72d Compare July 14, 2024 11:52
@cdragos cdragos marked this pull request as ready for review July 14, 2024 11:53
@cdragos cdragos changed the title chore: add pyright chore: Replace mypy with pyright for type checking and improve code quality Jul 14, 2024
@iurisilvio
Copy link
Contributor

Why pyright instead of mypy? I don't have strong opinions on which one is better, but I don't like to change tools without good reasons.

I'm happy we now have type checking, but I don't think changing tools will improve it a lot. I prefer to keep iterating on mypy config and enable many options still disabled.

@cdragos
Copy link
Contributor Author

cdragos commented Jul 14, 2024

@iurisilvio Thanks for your feedback. I apologize for not providing a description earlier.

I agree about being cautious with changing tools. While exploring pyright, I found it caught several issues with its default settings that the current setup missed:

  • Syntax errors like raise ("Model URL must be from either app.roboflow.com or universe.roboflow.com")
  • Runtime errors such as r = check_key(api_key) where the function signature doesn't match
  • Incorrect return type annotations, e.g., def train(..) -> bool: where the actual return type differs

One feature I found particularly useful is pyright's Inferred Return Types, similar to TypeScript. Unlike mypy, which marks unannotated return types as Any, pyright provides the actual return type.

Many more differences documented here made me think pyright is a better tool.

To move forward, I propose the following alternatives:

  1. Run both tools in parallel to evaluate which better suits our project needs
  2. Keep mypy and some changes / fixes from my PR

Let me know what you think!

@iurisilvio
Copy link
Contributor

iurisilvio commented Jul 15, 2024

I prefer to not add a new tool now. There is a lot of work improving mypy behaviour and another tool will split efforts with small gains. Can you go with option 2?

Copy link
Contributor

@iurisilvio iurisilvio left a comment

Choose a reason for hiding this comment

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

A few comments to ship this PR, considering we'll keep mypy.

@@ -282,7 +275,7 @@ def project(self, project_name, the_workspace=None):

dataset_info = dataset_info.json()["project"]

return Project(self.api_key, dataset_info)
return Project(self.api_key or "", dataset_info)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is the fix. api_key could be None for public projects.

I prefer to ignore the error instead of fix the type and cover a possible bug.

@@ -65,7 +65,7 @@ def __init__(
""" # noqa: E501 // docs
self.__api_key = api_key

def predict( # type: ignore[override]
def predict(
Copy link
Contributor

Choose a reason for hiding this comment

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

This is an example where mypy do better. This override with incompatible signature is bad.

I don't know how to fix it without breaking compatibility, but I like to have it annotated as an issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I disabled this from pyproject.toml. Pyright detected it with the standard configuration.

@@ -59,20 +59,13 @@ def check_key(api_key, model, notebook, num_retries=0):
return "onboarding"


def auth(api_key):
Copy link
Contributor

Choose a reason for hiding this comment

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

It is backward incompatible. I'd prefer to keep this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, but I am curious about the use case, at this moment the calling of this function raises an exception
TypeError: auth() missing 1 required positional argument: 'api_key'

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah ok , I didn't realized it is totally broken. 👍🏻 Yes, we can remove it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you remove it again? 😅 I reviewed all the PR already (all good), I'll wait for this last change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! Done!

@cdragos cdragos changed the title chore: Replace mypy with pyright for type checking and improve code quality chore: Enhance type checking with mypy and improve code quality Jul 16, 2024
@cdragos
Copy link
Contributor Author

cdragos commented Jul 16, 2024

@iurisilvio Thank you for your thorough feedback and guidance throughout this PR process. I've addressed all the comments raised. 🏄🏼

@cdragos cdragos requested a review from iurisilvio July 16, 2024 11:12
@iurisilvio iurisilvio merged commit 3750654 into roboflow:main Jul 16, 2024
6 checks passed
@iurisilvio
Copy link
Contributor

Thanks! 🚢

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