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

mypy: fix mypy errors in wladmin module. Rel: #3703 #12690

Merged
merged 1 commit into from
Oct 11, 2024

Conversation

SukiCZ
Copy link
Contributor

@SukiCZ SukiCZ commented Oct 5, 2024

Proposed changes

Fixes (7) mypy errors in wladmin module.

Checklist

  • Lint and unit tests pass locally with my changes.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added documentation to describe my feature.
  • I have squashed my commits into logic units.
  • I have described the changes in the commit messages.

Other information

list of fixed errors

weblate/wladmin/models.py:333:9: error: Name "support_status" already defined on line 331  [no-redef]
weblate/wladmin/sites.py:41:28: error: Argument 1 of "each_context" is incompatible with supertype "AdminSite"; supertype defines the argument type as "HttpRequest"  [override]
weblate/wladmin/sites.py:41:28: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
weblate/wladmin/sites.py:41:28: note: This violates the Liskov substitution principle
weblate/wladmin/views.py:326:18: error: Argument "key_type" to "get_key_data_raw" has incompatible type "str"; expected "Literal['rsa', 'ed25519']"  [arg-type]
weblate/wladmin/views.py:348:44: error: Argument "key_type" to "generate_ssh_key" has incompatible type "str"; expected "Literal['rsa', 'ed25519']"  [arg-type]
weblate/wladmin/views.py:432:21: error: Incompatible types in assignment (expression has type "str | list[object]", target has type "str | bytes")  [assignment]
weblate/wladmin/views.py:438:52: error: "Field" has no attribute "parser"  [attr-defined]
weblate/wladmin/views.py:506:16: error: "BillingManager" has no attribute "prefetch"  [attr-defined]

Copy link

codecov bot commented Oct 6, 2024

Codecov Report

Attention: Patch coverage is 90.90909% with 1 line in your changes missing coverage. Please review.

Project coverage is 91.19%. Comparing base (4aa7286) to head (6919a0f).
Report is 31 commits behind head on main.

Files with missing lines Patch % Lines
weblate/wladmin/models.py 50.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #12690      +/-   ##
==========================================
+ Coverage   91.15%   91.19%   +0.03%     
==========================================
  Files         596      596              
  Lines       61027    61031       +4     
  Branches     9660     6334    -3326     
==========================================
+ Hits        55629    55657      +28     
- Misses       3727     3728       +1     
+ Partials     1671     1646      -25     
Files with missing lines Coverage Δ
weblate/wladmin/sites.py 82.35% <100.00%> (ø)
weblate/wladmin/views.py 70.06% <100.00%> (+1.06%) ⬆️
weblate/wladmin/models.py 80.11% <50.00%> (ø)

... and 15 files with indirect coverage changes

weblate/billing/models.py Outdated Show resolved Hide resolved
@@ -12,7 +12,7 @@
from django.utils.translation import gettext, gettext_lazy

if TYPE_CHECKING:
from weblate.auth.models import AuthenticatedHttpRequest
from django.http import HttpRequest
Copy link
Member

Choose a reason for hiding this comment

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

The intention here is that Django's HttpRequest can have AnonymousUser what can't happen in Weblate. That's why AuthenticatedHttpRequest was introduced. Without that each access to reuqest.user has complains from mypy.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I've seen that answer in FAQ. The reason of this change is because of a method each_context, that has a signature with HttpRequest. The mypy error is:

error: Argument 1 of "each_context" is incompatible with supertype "AdminSite"; supertype defines the argument type as "HttpRequest" [override]
note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
note: This violates the Liskov substitution principle

For now I have two solutions on my mind, cast and ignore.

  • cast("AuthenticatedHttpRequest", request) doesn't make much sense, because the function doesn't actually need a request.user
  • # type: ignore[override] doesn't seem that bad actually

Copy link
Member

Choose a reason for hiding this comment

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

I was using ignore for override so far, IMHO it is the best approach

@nijel nijel self-assigned this Oct 11, 2024
@nijel nijel added this to the 5.8 milestone Oct 11, 2024
@nijel nijel merged commit cb14410 into WeblateOrg:main Oct 11, 2024
33 of 34 checks passed
@nijel
Copy link
Member

nijel commented Oct 11, 2024

Merged, thanks for your contribution!

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.

2 participants