-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: run mypy as part of testing the codebase
The edx-platform codebase already includes quite a few type annotations, but they were not regularly checked. This may cause problems, when the annotations themselves include errors (as we found out in some of the learning_sequences annotations). So here, we add mypy as a dev requirement and introduce a make command to run mypy regularly. Mypy runs on a very small portion of the total edx-platform, as configured in mypy.ini. Our hope is that developers will add more and more modules to this configuration file, until we can eventually run mypy on the full code base. See discussion: https://discuss.openedx.org/t/dev-notes-running-mypy-on-edx-platform/4860
- Loading branch information
Showing
15 changed files
with
131 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[mypy] | ||
follow_imports = silent | ||
ignore_missing_imports = True | ||
allow_untyped_globals = True | ||
exclude = tests | ||
files = openedx/core/djangoapps/content/learning_sequences/,openedx/core/types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
""" | ||
Add here typing utilities API functions and classes. | ||
""" | ||
from .admin import admin_display | ||
from .user import User |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
Typing utilities for the admin sites. | ||
""" | ||
import warnings | ||
|
||
from typing import Any, Callable, Optional, Protocol | ||
|
||
|
||
class AdminMethod(Protocol): | ||
""" | ||
Duck-type definition of a callable admin method. | ||
See: | ||
https://github.com/python/mypy/issues/2087#issuecomment-462726600 | ||
https://mypy.readthedocs.io/en/stable/protocols.html | ||
https://www.python.org/dev/peps/pep-0544/ | ||
""" | ||
|
||
short_description: str | ||
boolean: bool | ||
|
||
|
||
def _admin_display( | ||
boolean: Optional[bool] = None, description: Optional[str] = None | ||
) -> Callable[[Any], AdminMethod]: | ||
""" | ||
Decorator for functions that need to be annotated with attributes from AdminMethod. | ||
This method and the above AdminMethod class will no longer be necessary in Django 3.2, | ||
when `admin.display` is introduced: | ||
https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#django.contrib.admin.display | ||
""" | ||
|
||
def decorator(func: Any) -> AdminMethod: | ||
if boolean is not None: | ||
func.boolean = boolean | ||
if description is not None: | ||
func.short_description = description | ||
return func | ||
|
||
return decorator | ||
|
||
|
||
try: | ||
import django.contrib.admin | ||
|
||
admin_display = django.contrib.admin.display | ||
if _admin_display or AdminMethod: | ||
warnings.warn( | ||
( | ||
"Django 3.2+ available: the _admin_display method and the AdminMethod" | ||
"class should be removed from openedx.core.types" | ||
), | ||
DeprecationWarning, | ||
) | ||
except AttributeError: | ||
admin_display = _admin_display |
Oops, something went wrong.