Skip to content

Conversation

@michaeloliverx
Copy link

@michaeloliverx michaeloliverx commented Nov 1, 2023

Summary

Reduce overloads for get and aget and allow an unlimited number of service types to be returned.

Uses PEP 646 – Variadic Generics which means typing-extensions is required for python versions below 3.11.

Opened as a draft as mypy doesn't support TypeVarTuple and Unpack yet but support has been merged python/mypy#16354.

pyright has supported them for a while now.

Pull Request Check List

  • Typos aside (please, always submit typo fixes!), I understand that this pull request may be closed in case there was no previous discussion.
  • Do not open pull requests from your main branch – use a separate branch!
    • There's a ton of footguns waiting if you don't heed this warning. You can still go back to your project, create a branch from your main branch, push it, and open the pull request from the new branch.
    • This is not a pre-requisite for your your pull request to be accepted, but you have been warned.
  • Added tests for changed code.
    • The CI fails with less than 100% coverage.
  • New APIs are added to our typing tests at https://github.com/hynek/svcs/blob/main/tests/typing/.
  • Updated documentation for changed code.
    • New functions/classes have to be added to docs/core-concepts.md or one of the integration guides by hand.
    • Changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives.
      • The next version is the second number in the current release + 1. The first number represents the current year. So if the current version on PyPI is 23.1.0, the next version is gonna be 23.2.0. If the next version is the first in the new year, it'll be 24.1.0.
  • Documentation in .rst and .md files is written using semantic newlines.
  • Changes (and possible deprecations) are documented in the changelog.
  • Consider granting push permissions to the PR branch, so maintainers can fix minor issues themselves without pestering you.

@michaeloliverx
Copy link
Author

@hynek opened as a draft but what do you think?

@hynek
Copy link
Owner

hynek commented Nov 2, 2023

Wow if it worked reliably, that would be absolutely amazing.

The question is how breaking it would be to say that this feature only works with Mypy 1.7(?) 🤔

@michaeloliverx
Copy link
Author

The question is how breaking it would be to say that this feature only works with Mypy 1.7(?) 🤔

Hard question to answer I am not sure, you can actually enable some support in the current version with:

--enable-incomplete-feature=TypeVarTuple --enable-incomplete-feature=Unpack

Wow if it worked reliably, that would be absolutely amazing.

I think I jumped the gun a bit, its not working as I expected 😢, there doesn't seem to be a way to express (type[T]) -> T in the 2nd overload:

from typing import TypeVar, TypeVarTuple, Unpack, assert_type, overload

T = TypeVar("T")
Ts = TypeVarTuple("Ts")


@overload
def get(svc_type: type[T], /) -> T:
    ...


@overload
def get(*svc_types: Unpack[Ts]) -> tuple[Unpack[Ts]]:
    ...


def get(*svc_types: type) -> object:
    """
    Get services of *svc_types*.

    Instantiate them if necessary and register their cleanup.

    Returns:
            ``svc_types[0]`` | ``tuple[*svc_types]``: If one service is
            requested, it's returned directly. If multiple are requested, a
            tuple of services is returned.
    """
    ...


a = get(int)
assert_type(a, int)

a, b, c = get(int, str, bool)
assert_type(a, int)
assert_type(b, str)
assert_type(c, bool)

The return type is the same of what was passed i.e not an instance

example.py:36: error: Expression is of type "type[str]", not "str"  [assert-type]
example.py:37: error: Expression is of type "type[bool]", not "bool"  [assert-type]

Close but not correct, maybe I will ask in the mypy issue tracker if its possible to express such a type.

@hynek
Copy link
Owner

hynek commented Nov 2, 2023

Aw, good luck!

@michaeloliverx
Copy link
Author

Yes unfortunately it is a limit of the type system, python/mypy#16394.

Maybe some day.

@michaeloliverx michaeloliverx deleted the type-var-tuple branch November 3, 2023 10:07
@hynek
Copy link
Owner

hynek commented Nov 3, 2023

Thanks for trying tho; that would've been awesome!

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