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

Configure mypy #1243

Merged
merged 1 commit into from
Aug 18, 2022
Merged

Configure mypy #1243

merged 1 commit into from
Aug 18, 2022

Conversation

mvandenburgh
Copy link
Member

Closes #1232.

This makes the necessary changes to tox.ini to enable mypy static type-checking locally and in CI. Type-checking can be run via tox -e type.

Comment on lines +20 to +29
exclude = [
"^dandiapi/api/admin.py",
"^dandiapi/api/mail.py",
"^dandiapi/api/tests/",
"^dandiapi/api/management/",
"^dandiapi/api/migrations/",
"^dandiapi/api/models/",
"^dandiapi/api/tasks/",
"^dandiapi/api/views/",
]
Copy link
Member Author

Choose a reason for hiding this comment

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

For now, I've excluded all files and directories that have type errors. I'll make follow up PRs where I fix the type errors in each of these and remove the corresponding entry from exclude.

[tool.mypy]
ignore_missing_imports = true
show_error_codes = true
disable_error_code = ["attr-defined", "var-annotated"]
Copy link
Member Author

Choose a reason for hiding this comment

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

These two error cases produced a lot of type errors, many of which were incorrect because of django typing stubs not working. I've disabled them for now, and if django typing stubs ever get integrated with django-configurations properly, we can re-enable them.

Comment on lines +31 to +35
# Re-enable these when https://github.com/typeddjango/django-stubs/issues/417 is fixed.
# plugins = ["mypy_django_plugin.main", "mypy_drf_plugin.main"]

# [tool.django-stubs]
# django_settings_module = "dandiapi.settings"
Copy link
Member Author

Choose a reason for hiding this comment

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

Since we are using django-configurations (well, technically composed-configuration does, not dandi), there's some additional considerations for running these django plugins. There's an open issue to support it typeddjango/django-stubs#417, but it hasn't been implemented yet

Copy link
Collaborator

@brianhelba brianhelba Aug 15, 2022

Choose a reason for hiding this comment

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

DANDI does use django-configurations directly. That's why things like this: https://github.com/dandi/dandi-archive/blob/master/manage.py#L14 are necessary.

I agree that due to the lack of proper support, it's safer to disable these plugins for now.

tox.ini Outdated
skip_install = true
setenv =
DJANGO_CONFIGURATION = DevelopmentConfiguration
passenv =
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we were using the Mypy plugin, then the settings file would be getting parsed and these should be necessary. However, this shouldn't be necessary just to import all the files. Do you know what is requiring these?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah that's exactly why - I was using mypy_django_plugin previously, before I realized it didn't work with django-configuration - updated

Copy link
Collaborator

Choose a reason for hiding this comment

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

So can this be removed now?

Copy link
Member Author

Choose a reason for hiding this comment

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

My bad, I thought you were referring to the setenv section here, not passenv. But yes, passenv can also be removed now, I've pushed that change.

tox.ini Outdated Show resolved Hide resolved
tox.ini Outdated Show resolved Hide resolved
tox.ini Outdated Show resolved Hide resolved
Copy link
Collaborator

@brianhelba brianhelba left a comment

Choose a reason for hiding this comment

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

Open question about passenv in tox.ini.

tox.ini Outdated
deps =
mypy
django-stubs
djangorestframework-stubs
types-setuptools
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
types-setuptools

We shouldn't need this. We're not checking setup.py, we're looking directly inside dandiapi/.

Copy link
Member Author

Choose a reason for hiding this comment

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

We're using setuptools_scm for versioning in this file https://github.com/dandi/dandi-archive/blob/master/dandiapi/__init__.py. We could ignore it, but I don't think it hurts that much either to add it as a dependency. 🤷 I added it back as a dev extra dependency, but I'm fine with just excluding this one file too if installing types-setuptools is too overkill - any thoughts @brianhelba @AlmightyYakob ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I didn't realize you had that import of pkg_resources in your actual project module. I think it's correct to include types-setuptools for that. Sorry for my confusion.

@AlmightyYakob This is totally out of scope of this PR, but note there's a way to get the package version from Python 3.8+ standard libraries (rather than setuptools): https://github.com/pypa/setuptools_scm/#retrieving-package-version-at-runtime

tox.ini Outdated Show resolved Hide resolved
tox.ini Show resolved Hide resolved
Copy link
Collaborator

@brianhelba brianhelba left a comment

Choose a reason for hiding this comment

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

LGTM. :shipit:

@mvandenburgh Thanks for your perseverance in working out some of these low-level usage details. I'd like to upstream some of these change to the Girder 4 cookiecutter and I've learned a lot myself from this experience.

@mvandenburgh
Copy link
Member Author

I'll ping @waxlamp @danlamanna @AlmightyYakob @DeepikaGhodki here just so you all are aware of this change. In particular, note that this will now run in CI alongside the other tox checks, after linting and before the pytest tests.

@mvandenburgh mvandenburgh added the tests Add or improve existing tests label Aug 15, 2022
@mvandenburgh mvandenburgh merged commit ede6f0f into master Aug 18, 2022
@mvandenburgh mvandenburgh deleted the configure-mypy branch August 18, 2022 15:15
@dandibot
Copy link
Member

🚀 PR was released in v0.2.43 🚀

@dandibot dandibot added the released This issue/pull request has been released. label Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released This issue/pull request has been released. tests Add or improve existing tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Perform static type-checking on Python code
3 participants