Skip to content

Add lots of missing argument & return type hints #1204

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 10 commits into from
Oct 28, 2022

Conversation

intgr
Copy link
Collaborator

@intgr intgr commented Oct 26, 2022

Discovered by setting mypy options disallow_untyped_defs, disallow_incomplete_defs.

@intgr intgr force-pushed the add-lots-missing-func-hints branch from 7168897 to cd3061b Compare October 26, 2022 16:15
@intgr
Copy link
Collaborator Author

intgr commented Oct 26, 2022

I didn't fix all of the places -- there are tons more missing hints in django.contrib.gis and django.db.models.fields, so these settings can't be added to mypy.ini yet.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

I love and hate PRs like this :)

I love them because you did a great work annotating so many files and functions!

I hate them because I am not able to review them properly :(

I will read thought it, but I won't check every param / annotation with the source code. Please, forgive that!

Discovered by setting mypy options disallow_untyped_defs, disallow_incomplete_defs.
@intgr intgr force-pushed the add-lots-missing-func-hints branch from cd3061b to 6dfbade Compare October 26, 2022 16:17
@intgr
Copy link
Collaborator Author

intgr commented Oct 26, 2022

I hate them because I am not able to review them properly :(

I agree, there's too much stuff all over the place. But the idea is that eventually we can enable disallow_untyped_defs, disallow_incomplete_defs checks in CI so incomplete definitions could no longer be added.

_M = TypeVar("_M", bound=Model)
_ModelFormT = TypeVar("_ModelFormT", bound=ModelForm)

class BaseGenericInlineFormSet(BaseModelFormSet[_M, _ModelFormT]):
Copy link
Member

Choose a reason for hiding this comment

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

Should we add it to our monkeypatching? Since it is generic now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

BaseModelFormSet doesn't seem to be patched either.

@sobolevn
Copy link
Member

CC @adamchainz I really need a second pair of eyes 🙏

Copy link
Contributor

@adamchainz adamchainz left a comment

Choose a reason for hiding this comment

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

192 additions / 133 deletions is not too bad... had a quick look.

Is there any way to set the options in per-module settings in mypy.ini, for at least some modules?

@@ -33,7 +34,7 @@ class ModelBackend(BaseBackend):
is_active: bool = ...,
include_superusers: bool = ...,
obj: Optional[Model] = ...,
): ...
) -> QuerySet[AbstractBaseUser]: ...
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't this be with _AnyUser?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Won't work

django-stubs/contrib/auth/backends.pyi:37: error: Type argument "Union[AbstractBaseUser, AnonymousUser]" of "_QuerySet" must be a subtype of "Model"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This method does a DB query, so it only returns user models -- it can't return AnonymousUser.

Similar to how authenticate() returns AbstractBaseUser.

@@ -10,4 +10,4 @@ from django.db.migrations.loader import MigrationLoader as MigrationLoader

class Command(BaseCommand):
output_transaction: bool = ...
def execute(self, *args: Any, **options: Any): ...
def execute(self, *args: Any, **options: Any) -> Optional[str]: ...
Copy link
Contributor

Choose a reason for hiding this comment

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

did you check all management commands' execute / handle methods?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Why "all management commands"? This is just one command: sqlmigrate

I used the same return as BaseCommand.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I did check all functions that are missing return type annotations.

I didn't check all Command classes systematically in case they have differing return types, I think that's out of scope for this PR.

@intgr
Copy link
Collaborator Author

intgr commented Oct 27, 2022

Is there any way to set the options in per-module settings in mypy.ini, for at least some modules?

I tried, but couldn't get it to work. I suspect it might not work because the directory is called django-stubs, but module is actually django.

In any case I'll do a follow-up shortly that enables these checks globally.

@@ -45,7 +45,11 @@ class NestedObjects(Collector):
def nested(self, format_callback: Callable = ...) -> List[Any]: ...
def can_fast_delete(self, *args: Any, **kwargs: Any) -> bool: ...

def model_format_dict(obj: Union[Model, Type[Model], QuerySet, Options[Model]]): ...
class ModelFormatDict(TypedDict):
Copy link
Member

Choose a reason for hiding this comment

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

I think that this does not exist in django, so it is _ModelFormatDict

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ooops, sorry! I pushed this fix to the wrong branch. Will be fixed in #1206.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, no problem!

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

I think we should just merge it. We can fix any further problems later.

@sobolevn sobolevn merged commit 36044c9 into typeddjango:master Oct 28, 2022
@sobolevn
Copy link
Member

Thanks everyone! Major update!

@adamchainz
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants