Skip to content

Comments

Adds mypy annotations for templatetags#3422

Closed
mouse-reeve wants to merge 7 commits intomainfrom
mypy
Closed

Adds mypy annotations for templatetags#3422
mouse-reeve wants to merge 7 commits intomainfrom
mypy

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

@mouse-reeve
Copy link
Member Author

I'm going through and closing pull requests that are just too out of date. Please feel free to re-open or leave a comment if you have questions or want to revisit this ❤️

@mouse-reeve mouse-reeve closed this Feb 3, 2026
@mouse-reeve mouse-reeve deleted the mypy branch February 3, 2026 21:02
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