
Sorry, my message was a bit unclear. Eric has it right, I was talking about pytype's constraint solver, where unifying T | List[T] against List[str] gives us T = List[str] and List[T] = List[List[str]], but just because we considered the T first. I was wondering whether the other typecheckers also just considered the options in the order they were given, but I like Eric's description of a simplicity score. martin On Fri, Jul 8, 2022 at 7:47 PM Steven D'Aprano <steve@pearwood.info> wrote:
On Fri, Jul 08, 2022 at 05:33:46PM -0700, Martin DeMello via Typing-sig wrote:
Ran into an interesting user issue when unifying Union[T, List[T]] against List[str]. The binding of T depends on whether you express the type as Union[T, List[T]] or Union[List[T], T] since str matches List[str]. We've been doing the simplest thing and matching against the types in the order they appear in the union until the first one matches, but now I'm wondering if unions are conceptually unordered and we should use a smarter algorithm.
Hi Martin,
Sorry if I am missing something here, since I lack context (who is "we" in your post?) but the documentation is clear that argument union is ignored:
Union[S, T] = Union[T, S]
https://docs.python.org/3/library/typing.html#typing.Union
I don't understand what you mean by str matching List[str]. mypy does not do that, Given example.py:
``` from typing import List
def f(arg: List[str]) -> None: pass
def g(arg: str) -> None: pass
f("hello") g(["a", "b"]) ``` mypy reports both errors correctly:
``` example.py:10: error: Argument 1 to "f" has incompatible type "str"; expected "List[str]" example.py:11: error: Argument 1 to "g" has incompatible type "List[str]"; expected "str" Found 2 errors in 1 file (checked 1 source file) ```
-- Steve _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: mdemello@google.com