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

Adds mypy annotations for templatetags #3422

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

Adds mypy annotations for templatetags #3422

wants to merge 6 commits into from

Conversation

mouse-reeve
Copy link
Member

@mouse-reeve mouse-reeve commented Aug 28, 2024

Description

I figured I'd try and get used to typing, so I added some typing.

One thing I haven't figured out is how to handle definitions that take *args and can be called with miscellaneous arguments (maybe you know, @jderuiter?).

What type of Pull Request is this?

  • Bug Fix
  • Enhancement
  • Plumbing / Internals / Dependencies
  • Refactor

Does this PR change settings or dependencies, or break something?

  • This PR changes or adds default settings, configuration, or .env values
  • This PR changes or adds dependencies
  • This PR introduces other breaking changes

Details of breaking or configuration changes (if any of above checked)

Documentation

  • New or amended documentation will be required if this PR is merged
  • I have created a matching pull request in the Documentation repository
  • I intend to create a matching pull request in the Documentation repository after this PR is merged

Tests

  • My changes do not need new tests
  • All tests I have added are passing
  • I have written tests but need help to make them pass
  • I have not written tests and need help to write them

@mouse-reeve
Copy link
Member Author

@bookwyrm-social/code-review would be great, I am new to using type annotations

@mouse-reeve mouse-reeve changed the title Adds mypy annotatins for templatetags Adds mypy annotations for templatetags Aug 28, 2024
@jderuiter
Copy link
Contributor

One thing I haven't figured out is how to handle definitions that take *args and can be called with miscellaneous arguments (maybe you know, @jderuiter?).

Do you maybe have an example of this?

@dato
Copy link
Contributor

dato commented Sep 1, 2024

One thing I haven't figured out is how to handle definitions that take *args and can be called with miscellaneous arguments (maybe you know, @jderuiter?).

Do you maybe have an example of this?

I think this is about utils.cache.get_or_set(), which receives a callable, and then the arguments for that callable:

def get_or_set(
cache_key: str,
function: Callable[..., Any],
*args: Tuple[Any, ...],
timeout: Union[float, None] = None
) -> Any:
"""Django's built-in get_or_set isn't cutting it"""
value = cache.get(cache_key)
if value is None:
value = function(*args)
cache.set(cache_key, value, timeout=timeout)
return value

This can be typed more narrowly using typing.ParamSpec:

from typing import Callable, Optional, ParamSpec, TypeVar

Args = ParamSpec("Args")
Ret = TypeVar("Ret")

def get_or_set(
    cache_key: str,
    function: Callable[Args, Ret],
    *args: Args.args,
    timeout: Optional[float] = None
) -> Ret:
    ...

@mouse-reeve: you can pull this fix (and a couple other minor improvements) from this branch in my repo: mypy...dato:bookwyrm:push-ovrtyrmwkoxu. (I can also push here directly if you prefer!) With these three commits, the arg-type and misc disables are no longer needed.

@mouse-reeve
Copy link
Member Author

thank you! I think ... was the piece I was missing

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.

3 participants