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

Error when using Annotated with django-stubs and mypy #3263

Closed
JaeHyuckSa opened this issue Jul 10, 2024 · 2 comments
Closed

Error when using Annotated with django-stubs and mypy #3263

JaeHyuckSa opened this issue Jul 10, 2024 · 2 comments
Assignees
Labels

Comments

@JaeHyuckSa
Copy link

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.3.1

Steps to Reproduce

I am using django-stubs and mypy. However, I am failing to raise an error for some reason.

from typing import TypedDict, Annotated, Any

from django.db.models import Count, Exists, OuterRef
from django.db.models.query import QuerySet
from project.tags.models import (
    TagCommunityPost,
    TagCommunityPostContent,
    TagCommunityPostUserLike,
)
from project.users.models import User, UserTagScrapPost

tag_community_post_list = TypedDict(
    "tag_community_post_list",
    {
        "is_liked": bool,
        "is_scrapped": bool,
        "content": str,
        "comment_count": int,
    },
)

    
def get_queryset_by_community_id_and_user_for_post_list(
    self, tag_community_id: int, user: User
) -> QuerySet[Annotated[TagCommunityPost, tag_community_post_list]] | list[dict[str, Any]]:
    return (
        TagCommunityPost.objects.filter(tag_community_id=tag_community_id, tag_user_profile__user=user)
        .prefetch_related("image_urls")
        .select_related("tag_user_profile")
        .annotate(
            is_liked=Exists(
                TagCommunityPostUserLike.objects.filter(
                    tag_community_post_id=OuterRef("id"), tag_user_profile__user=user
                )
            ),
            is_scrapped=Exists(UserTagScrapPost.objects.filter(tag_community_post_id=OuterRef("id"), user=user)),
            content=TagCommunityPostContent.objects.filter(tag_community_post_id=OuterRef("id"))
            .order_by("id")
            .values("content")[:1],
            comment_count=Count("tagpostcomment"),
        )
    )

Versions

  • python: 3.11.5
  • django: 5.0.7
  • django-stubs: 5.0.2
  • mypy: 1.10.1

Expected Result

It should work normally.

Actual Result

Traceback (most recent call last):
  File "/Users/sajh/Documents/project/project-BE/venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
             ^^^^^^^^^^^^^^^
  File "/Users/sajh/Documents/project/project-BE/venv/lib/python3.11/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "mypy/main.py", line 100, in main
  File "mypy/main.py", line 182, in run_build
  File "mypy/build.py", line 192, in build
  File "mypy/build.py", line 266, in _build
  File "mypy/build.py", line 2942, in dispatch
  File "mypy/build.py", line 3333, in process_graph
  File "mypy/build.py", line 3414, in process_fresh_modules
  File "mypy/build.py", line 2110, in fix_cross_refs
  File "mypy/fixup.py", line 52, in fixup_module
  File "mypy/fixup.py", line 156, in visit_symbol_table
  File "mypy/fixup.py", line 72, in visit_type_info
  File "mypy/fixup.py", line 158, in visit_symbol_table
  File "mypy/nodes.py", line 897, in accept
  File "mypy/fixup.py", line 182, in visit_decorator
  File "mypy/nodes.py", line 787, in accept
  File "mypy/fixup.py", line 166, in visit_func_def
  File "mypy/types.py", line 1984, in accept
  File "mypy/fixup.py", line 265, in visit_callable_type
  File "mypy/types.py", line 2874, in accept
  File "mypy/fixup.py", line 358, in visit_union_type
  File "mypy/types.py", line 410, in accept
  File "mypy/fixup.py", line 252, in visit_type_alias_type
  File "mypy/types.py", line 1444, in accept
  File "mypy/fixup.py", line 230, in visit_instance
  File "mypy/fixup.py", line 367, in lookup_fully_qualified_typeinfo
  File "mypy/lookup.py", line 49, in lookup_fully_qualified
AssertionError: Cannot find component "WithAnnotations[ducpic__tags__models__TagCommunityPost, TypedDict('ducpic" for "django_stubs_ext.WithAnnotations[ducpic__tags__models__TagCommunityPost, TypedDict('ducpic.tags.types.tag_community_post_list', {'is_liked': builtins.bool, 'is_scrapped': builtins.bool, 'content': builtins.str, 'comment_count': builtins.int})]"
Sentry is attempting to send 2 pending events
Waiting up to 2 seconds
Press Ctrl-C to quit
@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Jul 10, 2024
@sl0thentr0py
Copy link
Member

@saJaeHyukc not sure I understand your problem. Is it that this error is not visible in sentry?

Otherwise it's just some typing issue with mypy and according to this you just need to delete .mypy_cache and try again.

@JaeHyuckSa
Copy link
Author

@saJaeHyukc not sure I understand your problem. Is it that this error is not visible in sentry?

Otherwise it's just some typing issue with mypy and according to this you just need to delete .mypy_cache and try again.

It works well as you suggested. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

3 participants