Skip to content

Add type annotations for mypy #746

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

Add type annotations for mypy #746

wants to merge 11 commits into from

Conversation

stevemessick
Copy link
Contributor

@stevemessick stevemessick commented Apr 24, 2025

I moved FileList from the bottom of the file to the top.

I introduced a number of typed variables to represent the return type. Not sure why, but mypy claimed they had type Any otherwise. This is mostly in the response objects, which are defined in kagglesdk, and I don't type-check that library because it has a lot of errors. That may have something to do with it.

The lookup_enum() method exists for legacy code/backward compatibility. To eliminate a bunch of casts, I added a parameter that is a sample value of the enum that needs to have a value extracted.

Type checking uncovered some errors in code I haven't tested. I kinda had to fix them to move on. Likewise, I noticed a number of docstring comments that needed some work, so I did it.

@stevemessick stevemessick marked this pull request as draft April 24, 2025 23:02
Copy link
Contributor

@rosbo rosbo left a comment

Choose a reason for hiding this comment

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

Thanks. A few comments.

.gitignore Outdated
@@ -65,3 +65,5 @@ target/

# Rider/IntelliJ
.idea/

mypy
Copy link
Contributor

Choose a reason for hiding this comment

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

I looked in kagglehub and we ignore the following:

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

https://github.com/Kaggle/kagglehub/blob/main/.gitignore#L141C1-L144C11

I doubt mypy writes to a file named mypy...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suppose I could remove that. I added it just to have a place to stash mypy stuff, like the html report and error output. I'm still running it manually, but eventually will script hatch to do it.

else:
group = self.lookup_enum(CompetitionListTab, group)
group_val = cast(CompetitionListTab, self.lookup_enum(CompetitionListTab, group))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can lookup_enum return a type based on the type of it's first argument? Similar to generics in Java. This may solve the cast issue but I am not sure that's possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to make that happen but was not able to figure it out. Since then, I have seen a reference to generics in the mypy docs, so I'll try again.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like it could be made to work, if we switch to 3.12. The TypeVar syntax isn't available in prior versions. I don't think we can make that switch, but I'm going to see if I can do it anyway, as an exercise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could not find a way to specify that the return type was an instance of a class-type parameter. I ended up adding an additional parameter to the method, which is a sample of the correct type. Looks like this:

  request.framework = self.lookup_enum(ModelFramework, ModelFramework.MODEL_FRAMEWORK_API, framework)

Not ideal, but it will do.

With a little work in the code that prompts users to upgrade, it seems like we can switch to requiring Python 3.12.

pyproject.toml Outdated
"idna",
"python-dateutil>=2.5.3",
"python-slugify",
# "certifi>=14.05.14",
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you need to comment these out? Is it because they are now unused?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's WIP. There was a lot of stuff that Swagger needed that we don't need. I haven't tested to see if the stuff I'm planning to remove is actually unused, but intend to.

)'''
python_version = 3.12

# Start off with these
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the goal to eventually use the default mypy config like we do for kagglehub?

https://github.com/Kaggle/kagglehub/blob/b7317a2448f68e766e04a05718ad55823fc35a65/pyproject.toml#L91

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, if I have time. The commented-out stuff comes from the mypy docs. Again, WIP.

@stevemessick
Copy link
Contributor Author

Converting to Python 3.12 will require some work here to get integration tests working.

@stevemessick
Copy link
Contributor Author

The integration tests fail:

Starting Step #1 - "integration-tests"
Step #1 - "integration-tests": Pulling image: us-docker.pkg.dev/kaggle-cicd/tools/hatch:3.12
Step #1 - "integration-tests": Error response from daemon: manifest for us-docker.pkg.dev/kaggle-cicd/tools/hatch:3.12 not found: manifest unknown: Failed to fetch "3.12"

I think that means we need a new docker image that uses Python 3.12. Not sure how to fix that.

@stevemessick
Copy link
Contributor Author

For now, using 3.11 syntax and all checks pass.

@stevemessick stevemessick marked this pull request as ready for review April 28, 2025 22:31
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