Skip to content

Conversation

UnknownPlatypus
Copy link
Contributor

I have made things!

(Review commit per commit should be easier)

Followup to #2852, adding a few ruff rules I often use.

UnknownPlatypus and others added 9 commits October 5, 2025 13:26
TC006 will also make minor perf improvement

> In some cases where cast() is used in a hot loop, this rule may also help avoid overhead from repeatedly evaluating complex type expressions at runtime.

class PersonFieldsetListAdmin(admin.ModelAdmin[Person]):
fieldsets = [
fieldsets = [ # noqa: RUF012
Copy link
Member

Choose a reason for hiding this comment

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

I propose to ignore this rule

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know, I feel it can catch future errors but sadly because of the ClassVar / TypeVar limitation, these few admin fields are not declared as Classvar. and trigger this rule.

What about ignoring for files matching the glob "*/admin*.py" ?

Copy link
Member

Choose a reason for hiding this comment

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

These are tests, they are not real code. I don't see a valid reason to force this rule on tests files at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ye that make sense, but we probably still want it in the python plugin code for ex.

Copy link
Collaborator

Choose a reason for hiding this comment

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

So, ignore for tests using [tool.ruff.lint.per-file-ignores]?


def get_django_metadata(model_info: TypeInfo) -> DjangoTypeMetadata:
return cast(DjangoTypeMetadata, model_info.metadata.setdefault("django", {}))
return cast("DjangoTypeMetadata", model_info.metadata.setdefault("django", {}))
Copy link
Collaborator

@intgr intgr Oct 5, 2025

Choose a reason for hiding this comment

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

Nit: Not opposed to this, but my first instinct is that these changes reduce readability.

From Ruff docs runtime-cast-value (TC006) https://docs.astral.sh/ruff/rules/runtime-cast-value/#why-is-this-bad

In some cases where cast() is used in a hot loop, this rule may also help avoid overhead from repeatedly evaluating complex type expressions at runtime.

I guess that's a point sometimes. But doesn't really apply when the type expression is a simple type reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nit: Not opposed to this, but my first instinct is that these reduce readability.

Agree, but I'm even wondering if we should really be using cast in these cases, a type: ignore makes more sense here for example and completely removes the function call

The perf argument does seem compelling since we might be in hot loops when running mypy

@intgr intgr changed the title Add more ruff rules Enable more ruff rules Oct 5, 2025
@sobolevn sobolevn merged commit 36336cb into typeddjango:master Oct 5, 2025
39 checks passed
@UnknownPlatypus UnknownPlatypus deleted the add-more-ruff-rules branch October 5, 2025 16:19
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