-
Hey there, I stumbled across some pyright type narrowing errors and was a bit surprised. I'm in dire need of some enlightenment. Infos:
Inline conditioncondition = "some-str"
narrowing = "" if condition else 1
reveal_type(narrowing)
# From Pyright: Type of "narrowing" is "Literal[1, '']"
# From runing: Runtime type is 'str'
# From Mypy: Revealed type is "builtins.object" condition = "some-str" # same typing result with: [0, 1, True, False, object()]
narrowing = "" if condition is not None else 1
reveal_type(narrowing)
# From Pyright: Type of "narrowing" is "Literal[1, '']"
# From runing: Runtime type is 'str'
# From Mypy: Revealed type is "builtins.str" condition = "some-str" # same typing result with: [0, 1, True, False, object()]
narrowing = "" if bool(condition) else 1
reveal_type(narrowing)
# From Pyright: Type of "narrowing" is "Literal[1, '']"
# From runing: Runtime type is 'str'
# From Mypy: Revealed type is "builtins.object" Note: same typing result with all - any on sequencesimport dataclasses
@dataclasses.dataclass
class Config:
token: str | None = None
cfg = Config(token="secret")
if all((cfg.token,)):
reveal_type(cfg.token)
# From Pyright: Type of "cfg.token" is "str | None"
# From runing: Runtime type is 'str' The questionIs it a bug in pyright ? |
Beta Was this translation helpful? Give feedback.
Answered by
erictraut
Jul 16, 2022
Replies: 1 comment 5 replies
-
All of your examples are working as designed, so I don't see any bugs here. Is there a particular case that you're wondering about? |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
gjeusel
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
All of your examples are working as designed, so I don't see any bugs here. Is there a particular case that you're wondering about?