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

refactor: Support type checking from cache_for decorator #26052

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

tkaemming
Copy link
Contributor

@tkaemming tkaemming commented Nov 7, 2024

Problem

cache_for is a type checking black hole.

This makes it easy to introduce errors when refactoring things like get_materialized_columns which use the decorator.

Changes

  • Updates type annotations for cache_for so that parameters and return types can be type checked.
  • Refactors internal implementation to CachedFunction class because mypy doesn't like it when you stuff additional attributes onto function types.

Does this work well for both Cloud and self-hosted?

Yes.

How did you test this code?

mypy

@tkaemming tkaemming changed the title refactor: Support type checking return values from cache_key decorator refactor: Support type checking return values from cache_for decorator Nov 7, 2024
@tkaemming tkaemming changed the title refactor: Support type checking return values from cache_for decorator refactor: Support type checking from cache_for decorator Nov 7, 2024
Comment on lines +72 to +73
current_materialized_column_name = materialized_columns.get((property_name, "properties"), None)
if current_materialized_column_name is not None and current_materialized_column_name != property_name:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This has been broken for a while

Comment on lines +604 to +606
posthog/year_in_posthog/year_in_posthog.py:0: error: Item "None" of "dict[Any, Any] | None" has no attribute "get" [union-attr]
posthog/year_in_posthog/year_in_posthog.py:0: error: Argument 1 to "stats_for_user" has incompatible type "dict[Any, Any] | None"; expected "dict[Any, Any]" [arg-type]
posthog/year_in_posthog/year_in_posthog.py:0: error: Item "None" of "dict[Any, Any] | None" has no attribute "get" [union-attr]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are already "handled" here and don't want to fix them

except Exception as e:
capture_exception(e)
logger.error("year_in_posthog_2023_error_rendering_2023_page", exc_info=True, exc=e, data=data or "no data")
template = get_template("hibernating.html")
html = template.render({"message": "Something went wrong 🫠"}, request=request)
return HttpResponse(html, status=500)

@@ -333,12 +333,14 @@ ee/billing/billing_manager.py:0: error: Incompatible types in assignment (expres
posthog/models/property/util.py:0: error: Incompatible type for lookup 'pk': (got "str | int | list[str]", expected "str | int") [misc]
posthog/models/property/util.py:0: error: Argument 3 to "format_filter_query" has incompatible type "HogQLContext | None"; expected "HogQLContext" [arg-type]
posthog/models/property/util.py:0: error: Argument 3 to "format_cohort_subquery" has incompatible type "HogQLContext | None"; expected "HogQLContext" [arg-type]
posthog/models/property/util.py:0: error: Invalid index type "tuple[str, str]" for "dict[tuple[str, Literal['properties', 'group_properties', 'person_properties']], str]"; expected type "tuple[str, Literal['properties', 'group_properties', 'person_properties']]" [index]
Copy link
Contributor Author

@tkaemming tkaemming Nov 7, 2024

Choose a reason for hiding this comment

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

These aren't particularly problematic and trying to fix them just moves the errors up the stack to all the call sites instead and don't want to deal with them right now

@tkaemming tkaemming marked this pull request as ready for review November 7, 2024 13:53
@tkaemming tkaemming requested a review from a team as a code owner November 7, 2024 13:53
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.

1 participant