ReactiveX / RxPY

ReactiveX for Python

Home Page:https://rxpy.rtfd.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Type-var `_T` in `Observable[_T]` should be covariant

Azureblade3808 opened this issue · comments

The only type-var _T of Obserable is currently declared as invariant and is causing false positive with the type-checker I'm using (namely Pylance/Pyright).

As we can see, this type-var only describes output values, and it works just like _T_out of rx.abc.ObservableBase, so there is little reason to make it invariant.

The current typing is clearly forbiding ops.switch_latest() being applied to a proper observable. switch_latest accepts a Observable[Observable[_T] | Future[_T]] as its argument. With _T in Observable[_T] being invariant, the original observable is required to emit values with type of exactly Observable[?] | Future[?], which is very unlikely.

To Reproduce
Let Pylance/Pyright check the following sample code.

Expected behavior
The sample code should pass type checks.

Code or Screenshots

import reactivex as rx, reactivex.operators as ops

a: rx.Observable[rx.Observable[int]] = globals()[""]
b = a.pipe(ops.switch_latest())

image

Additional context
Add any other context about the problem here.

  • OS [e.g. Windows]
    Doesn't matter.

  • RxPY version [e.g 4.0.0]
    4.0.2

  • Python version [e.g. 3.10.2]
    Doesn't matter.