on-internet
← Community

Can I make Pyright catch unnecessarily broad type annotations?

Stack Overflow ·

Pyright warns me when the annotated return type of a function is narrower than the actual return type. However, it doesn't warn me when it's broader. Example: class User: def __init__(self, id: int) -> None: self.id = id users: list[User] = [] def get_user(id: int) -> User | None: # forgot to remove `| None` user = next((u for u in users if u.id == id), None) if user is None: raise ValueError(f"user {id} not found") return user I'd changed the body of get_user() to always return a User, but had forgotten to update the annotation. Pyright doesn't warn me that User | None could be narrowed to Us

Comments

Sign in or sign up to comment and upvote.

No comments yet.