ITADN

spec/conformance: function with TypeVar default and default argument used in context.

#2213Openrandolf-scholz 创建于 2026-03-08
topic: other
R
randolf-scholzcommented
I want to suggest adding the following test to the conformance test suite: ```python class Getter[T]: def get[S=None](self, default: S = None) -> T | S: ... # OK def test(arg: Getter[str]) -> None: result: str = arg.get() # type: ignore ``` - Since the default argument value is assignable to the default type var type, `def get` is well-defined - `arg.get()` implicitly uses `None` of type `None` as the argument. Therefore, we have an implicit constraint `S <: None` when `.get` is called without explicit default. Therefore, `result: str = arg.get()` should raise an `assignment` type error (`str | None` not assignalbe to `str`). Currently, only `pyright` succeeds on this test, and type checker results diverge. 1. `pyright`: no errors (true negative on `def get`, true positive on `result`) ([pyright playground](https://pyright-play.net/?code=MYGwhgzhAEDiCmAXR8BOBtAKgXQFwChojoATeAM2gHMl0BlAXgDkB7AO3mwAoJ4RyANKQpgAriES5odaA2isOASmgBaAHzRM0AD7SpAOkNEAxNADyAaXz4ylFBERcwqKlITI06B6mzL189ngCYmhUeAhxSWhvWWhnKn0aR2VoUwBRKRjdBXhoNhZEOKgASyo2MAAjEFzEFmjEVCA)) 2. `mypy`: false positive on `def get`, false negative on `result`. 3. `ty`: false positive on `def get`, true positive on `result`. ([playground](https://play.ty.dev/e4eb6689-da48-40b8-8f99-6ce516a407a1)) 4. `pyrefly`: false positive on `def get`, false negative on `result` ([playground](https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIAxlKnHAAQDiMALizAE4DaAKgLqIAOunqj6mGGHoBzVlwDKAXgByudDD4AKODChgANOMmoArlBaJ68%2Bovqr1ASnoBaAHz0e9AD5XLhf6IAxPQA8gDSwsISUuxwLJqoHNKWzGycXHEcfE5udmowQiJiHPBmFvSZNvSJ0oSy8U70wQCilpU%2B9jD06Lgs1XQQ0uio2LD0LLgVLBwg%2BiAmLNBwJOSIIMEAqotQECyk9GAm6JSLanCRWJIHuBwAtqgsAProJrfYnJr4lhDoLDnumUKYnoJRYJg4IjAghAyle7w4lmA%2BAAvtDhLMQGQSmAoKRCBNblAKMEAAqkbG4ioYHAEeiUNSQaTgh4QNSEYTBeQwLoACzYxDgiAA9EKsZJcYQbtIhTB0ELMLhKHAhfT0Izmac5dcONUAG6oaAjMaq9UcFlqei4Yia5bCMgsHlqZy6zhwVkiWzQgDMhAAjF60egQMi5qgThAXQAxaAwChoLB4IhkYNAA)) 5. `zuban`: false postive on `def get`, false negative on `result`. ## Related Issues - https://github.com/python/mypy/issues/3737 (one of the most upvotes issues on mypy) - https://github.com/astral-sh/ty/issues/592
3 条评论