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

Added Type Hints #1282

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft

Added Type Hints #1282

wants to merge 23 commits into from

Conversation

smithdc1
Copy link
Member

@smithdc1 smithdc1 commented Jul 1, 2022

Very speculative, and I'm not even sure we want this, but I've enjoyed the learning journey.

Needs more thought, tidying up, and splitting out into smaller chunks (like the first commit here).

Also likely far too many # ignore at the moment. Although some of those are now just waiting for a new release of Django-stubs following a series of contributions there.

So yes, still loads to do.

But tests pass (at least locally), so there's that!

@codecov-commenter
Copy link

codecov-commenter commented Jul 1, 2022

Codecov Report

Merging #1282 (c83675d) into main (3b5844e) will decrease coverage by 2.93%.
The diff coverage is 84.35%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1282      +/-   ##
==========================================
- Coverage   89.30%   86.37%   -2.94%     
==========================================
  Files          12       12              
  Lines        1010     1101      +91     
  Branches      192      201       +9     
==========================================
+ Hits          902      951      +49     
- Misses         78      111      +33     
- Partials       30       39       +9     
Flag Coverage Δ
unittests 86.37% <84.35%> (-2.94%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
crispy_forms/templatetags/crispy_forms_tags.py 91.26% <60.00%> (-6.15%) ⬇️
crispy_forms/base.py 73.33% <62.50%> (-16.67%) ⬇️
crispy_forms/templatetags/crispy_forms_utils.py 87.50% <66.66%> (-12.50%) ⬇️
crispy_forms/templatetags/crispy_forms_filters.py 84.84% <72.72%> (-6.38%) ⬇️
crispy_forms/utils.py 86.31% <73.91%> (-5.64%) ⬇️
crispy_forms/templatetags/crispy_forms_field.py 87.09% <82.14%> (-4.77%) ⬇️
crispy_forms/bootstrap.py 80.88% <87.17%> (-1.08%) ⬇️
crispy_forms/layout.py 83.58% <89.36%> (-1.16%) ⬇️
crispy_forms/helper.py 91.44% <94.36%> (-1.24%) ⬇️
crispy_forms/layout_slice.py 89.15% <96.29%> (+0.40%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3b5844e...c83675d. Read the comment docs.

@smithdc1 smithdc1 force-pushed the types branch 3 times, most recently from a9a1794 to 40cb9ff Compare July 6, 2022 20:38
self.field = field
self.attrs = attrs

def render(self, context):
def render(self, context: Context) -> str:
Copy link
Member Author

@smithdc1 smithdc1 Jul 6, 2022

Choose a reason for hiding this comment

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

Not SafeString 🤔 ?

Comment on lines 3 to 4
from types import TracebackType
from typing import Collection, Union
Copy link

Choose a reason for hiding this comment

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

If you wrap these inside if typing.TYPE_CHECKING: they will only be imported on mypy runs.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the review. 👍

Any other comments on this would be much appreciated. 🙏

@pawelad pawelad mentioned this pull request Jul 7, 2022
@smithdc1 smithdc1 linked an issue Jul 8, 2022 that may be closed by this pull request
Comment on lines 100 to 101
template_pack: Union[SimpleLazyObject, str] = TEMPLATE_PACK,
extra_context: Optional[Dict[str, Any]] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd create named type aliases for these kind of things, which pop up a lot.

🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. Also I think there will be a lot of "dict contexts". So something like dict[str | int | Node, Any].

I found out this week that dict[str, Any] is not compatible with dict[str | int | Node, Any]. Which I found surprising.

self,
form: BaseForm,
context: Context,
template_pack: Union[str, SimpleLazyObject] = TEMPLATE_PACK,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here's this one again, but the other way round.

(Maybe LazyStr 🤔)

Copy link
Member Author

Choose a reason for hiding this comment

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

I need to look at this. 👍

I don't really understand what a simplelazyobject is or why it's used.

@@ -9,14 +19,19 @@ class KeepContext:
touch context object themselves, that could introduce side effects.
"""

def __init__(self, context, keys):
def __init__(self, context: Context, keys: Collection[int | str | Node]) -> None:
Copy link
Member Author

Choose a reason for hiding this comment

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

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.

Type hints
4 participants