Note: This week's release includes some major changes related to TypeVar constraint solving that have been in the works for a while. These changes simplify the code, make constraint solving behaviors more consistent, and eliminate a number of false positive errors that were reported over the past year. However, these code changes produce different constraint solving behaviors in some cases. These extensive code changes also come with the potential of regressions. I've done extensive analysis of the type checking outputs across over a hundred public code bases, but it's possible that I've missed something. Please report any new behavior that you think is a bug.
Bug Fixes:
- Fixed bug in logic that validates subtyping relationships between
TypeIs[T]
,TypeGuard[T]
andbool
when used in a return type of a callable. - Fixed bug that results in a crash if
Optional
is used with no subscript (i.e.Optional[]
). - Fixed bug in
isinstance
type narrowing if the second argument isobject
and the first argument is atype
instance. - Fixed a bug that results in a crash in certain conditions involving a corrupt builtins.pyi stub.
- Fixed recent regression that results in incorrect
isinstance
type narrowing when the filter class and the type are both protocols. - Fixed bug that results in a warning if the
self
parameter in an__init__
method is given an explicitSelf
annotation. - Fixed recent regression that results in a false positive error when evaluating certain nested constructor calls when used with bidirectional type inference.
- Fixed false positive error when iterator returns a class object from its
__iter__
method. - Fixed recent regression that results in a false positive error under certain circumstances when yield expression includes a call to a constructor.
- Fixed a bug that results in an incorrect "inconsistent overload" error when the overloads return
TypeIs
orTypeGuard
and the implementation returnsbool
. - Fixed a bug that results in incorrect type narrowing when tuples are used in conjunction with
TypeIs
. - Fixed a bug that can result in incorrect type evaluation when an
isinstance
type narrowing results in an intersection type. - Fixed bug that results in a false negative if a
__set__
descriptor method is marked deprecated and is implicitly accessed using an augmented assignment operator, as ina.x += 1
. - Fixed bug that can result in a false negative when a function return type is a TypeVar and the function falls through and implicitly returns a
None
. - Fixed bug that can result in a false positive when calling the constructor within a class that has unannotated
__init__
or__new__
method parameters. - Fixed bug that results in false positive error under certain circumstances when solving type variables that involve literal values.
- Fixed bug that results in a false positive when using a two-argument form of
super()
outside of a class.
Enhancements:
- Improved signature help for constructor calls. Replaced old heuristics that displayed either the
__init__
or__new__
signature with a mechanism that uses the recently-ratified typing spec algorithm for converting a constructor into a callable. This is prompted in part by this discussion. - Added error check for an enum attribute with a "naked"
Final
attribute. This should be considered invalid. - Added a new configuration option
enableReachabilityAnalysis
. It is off by default whentypeCheckingMode
is "off" but otherwise on by default. When disabled, it causes pyright not to identify code blocks that are determined to be unreachable via type analysis. Code blocks that are determined to be unreachable via non-type information are still displayed as such. - Enhanced type narrowing for sequence patterns to support tuple expansion when the subject is a tuple whose entries are union types.
- Added missing check for
Final
variable assigned in a loop. - Added support for
@deprecated
decorator on magic methods for unary and binary operations. - Added support for
@deprecated
on__bool__
magic method used bynot
operator. - Added support for
@deprecated
on__new__
methods used implicitly during a class constructor call.
Other Changes:
- Changed evaluation behavior for TypeVar bounds, constraints, and defaults to enforce type expression evaluation rules, consistent with the typing spec.
- Eliminated error condition when using a subscript expression for a generic class that does not conform to type expression rules (e.g.
list[1 + 2]
) if the expression is a value expression. - Modified heuristics in
T is None
type narrowing logic to handle TypeVars with no bounds better. The previous logic was arguably correct, but it produced results that were unexpected by some users. - Adjusted the heuristics for constraint solving to favor solutions of type
T
overtype[T
]` when both are valid.