Skip to content

False positive with a descriptor inside a union in django-stubs #2817

Description

@sobolevn

I've faced this problem in my project: https://github.com/wemake-services/django-modern-rest/blob/5f809b224fb78b07d23f339cb3f3d25c0835b4c7/dmr/openapi/generators/component_parsers.py#L134-L139

For some reason pyrefly reports an attribute as missing. While mypy and pyright don't. Runtime also does not have any issues.

Minimal repro:

import re
from typing import reveal_type

class LocaleRegexRouteDescriptor:
    def __get__(self, instance: 'RoutePattern | None', cls: type | None = None) -> re.Pattern[str]: ...

class LocaleRegexPattern:
    @property
    def regex(self) -> re.Pattern[str]: ...

class RegexPattern:
    regex: re.Pattern[str]

class RoutePattern:
    regex: LocaleRegexRouteDescriptor

class URLRoute:
    pattern: RegexPattern | RoutePattern | LocaleRegexPattern

route = URLRoute()
reveal_type(route.pattern.regex.groupindex)

Error:

» pyrefly check ex.py
 INFO revealed type: Unknown [reveal-type]
  --> ex.py:21:12
   |
21 | reveal_type(route.pattern.regex.groupindex)
   |            --------------------------------
   |
ERROR Object of class `LocaleRegexRouteDescriptor` has no attribute `groupindex` [missing-attribute]
  --> ex.py:21:13
   |
21 | reveal_type(route.pattern.regex.groupindex)
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
 INFO 1 error

mypy:

ex.py:21: note: Revealed type is "typing.Mapping[builtins.str, builtins.int]"

Full stubs: https://github.com/typeddjango/django-stubs/blob/9b6453a164f7791dae9cca8ae72672179b83afbe/django-stubs/urls/resolvers.pyi#L89-L90

After the fix a test with the type assertion is very welcomed in django-stubs repo :)
Since it affects our users.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions