Skip to content

v1.22.0 (pyright 1.1.389)

Latest
Compare
Choose a tag to compare
@DetachHead DetachHead released this 23 Nov 11:49
· 20 commits to main since this release

What's Changed

improved narrowing for generic types

when narrowing a type using an isinstance check, there's no way for the type checker to narrow its type variables, so pyright just narrows them to "Unknown":

def foo(value: object):
    if isinstance(value, list):
        reveal_type(value) # list[Unknown]

this makes sense in cases where the generic is invariant and there's no other way to represent any of its possibilities. for example if it were to be narrowed to list[object], you wouldn't be able to assign list[int] to it. however in cases where the generic is covariant, contravariant, or uses constraints, it can be narrowed more accurately.

this release introduces the new strictGenericNarrowing setting to address this:

class Foo[T_co, T_contra]:
    def foo(self, value: T_contra) -> T_co: ...

def foo(value: object):
    if isinstance(value, Foo):
        reveal_type(value) # previously `Foo[Any, Any]`, now `Foo[object, Never]`

for more information, see the docs.

huge thanks to @beauxq for the help on this. implemented in #745

new diagnostic rule - reportExplicitAny

similar to reportAny, however this rule bans usages of the Any type itself rather than expressions that are typed as Any:

def foo(baz: Any) -> Any: # error: reportExplicitAny
    print(baz) # error: reportAny

implemented in #83

other changes

  • Chinese (Simplified) localization update (2024.11) by @NCBM in #879
  • Fix duplicated strictListInference in the docs by @rbrgmn in #888
  • show Never instead of NoReturn when it's inferred as the return type of a function by @peacewalker122 in #895
  • document Never and NoReturn by @DetachHead in #899

New Contributors

Full Changelog: v1.21.1...v1.22.0