Skip to content

Typing issues #53

Closed
Closed

Description

@Dreamsorcerer

The Callable should be able to be tightened up on line 43.
So, that can probably become Callable[[_TSelf[_T]], _T].

However, looking at that, it's defined the _cache as _cache: Dict[str, _T]. Wouldn't that make it impossible to use this decorator multiple times on different values? e.g. If I have a class with a URL and a port as int, then the dict type would need to be dict[str, URL | int], which would then result in both attributes returning URL | int.

So, I think the _cache annotation might need some reworking... I suspect the Generic will need to be dropped from _TSelf and the dict may need to use Any. I'm not sure there's a clear way to do it accurately and type safe.

Originally posted by @Dreamsorcerer in #50 (comment)

Activity

  1. Dreamsorcerer commented on Oct 8, 2024

    @Dreamsorcerer
    MemberAuthor

    Following on from that, I tried adjusting the type of the Callable, but just seem to get errors (maybe because the class hasn't been fully defined at the time of the decorator, mypy doesn't recognise the class as matching the protocol?).

    However, a quick test also revealed that the typing information is completely broken.
    We need to add some assert_type()s to the tests and fix the type errors.

    Adjusting the first test to:

    def test_under_cached_property(propcache_module: APIProtocol) -> None:
        class A:
            def __init__(self) -> None:
                self._cache: Dict[str, int] = {}
    
            @propcache_module.under_cached_property
            def prop(self) -> int:
                return 1
    
            @propcache_module.under_cached_property
            def prop2(self) -> str:
                return "foo"
    
        a = A()
        assert_type(a.prop, int)
        assert a.prop == 1
        assert_type(a.prop2, str)
        assert a.prop2 == "foo"

    Reveals that all properties are Any. I'd also expect to get another error about the dict type, but currently don't.

  2. added
    enhancement
    help wanted
    on Oct 8, 2024
  3. Dreamsorcerer commented on Oct 8, 2024

    @Dreamsorcerer
    MemberAuthor

    Hmm, the typing seems to work on aiohttp though, so I'm not sure what I'm doing differently here for it to not work...

  4. webknjaz commented on Oct 8, 2024

    @webknjaz
    Member

    Different MyPy version?

  5. Dreamsorcerer commented on Oct 8, 2024

    @Dreamsorcerer
    MemberAuthor

    I was testing locally with the same instance of mypy on each repo. assert_type() on req.rel_url in an aiohttp test works correctly, but in the above test it fails with Any.

  6. webknjaz commented on Oct 8, 2024

    @webknjaz
    Member

    Any future import of annotations anywhere?

  7. Dreamsorcerer commented on Feb 26, 2025

    @Dreamsorcerer
    MemberAuthor

    I started on the yarl strict typing and this is definetely a problem, I get:

    yarl/_url.py:478:21: error: No overload variant of "__get__" of "under_cached_property" matches argument types "URL", "type[URL]"
    

    And it resulted in an extra 800 type errors.

    I'll have to fix up propcache before I can go back to yarl.

  8. Dreamsorcerer commented on Feb 26, 2025

    @Dreamsorcerer
    MemberAuthor

    Reveals that all properties are Any. I'd also expect to get another error about the dict type, but currently don't.

    I think the reason this is broken and why we don't see any real type errors is because of the pytest fixtures. I don't think there's a way to properly declare that we have the function itself. The current annotations of type[under_cache_property] want a subscripted type, without it mypy just infers type[under_cache_property[Any]] and therefore we don't get any proper type checking.

    Might have to figure out something more awkward to use the imported function directly when type checking...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementhelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions