pyright icon indicating copy to clipboard operation
pyright copied to clipboard

bug: spurious "partially unknown"

Open bukzor opened this issue 1 year ago • 7 comments

Describe the bug

I don't understand this bug at all, but I can tell you how to reproduce it, and I think it's clearly a bug.

Setup:

$ tail -vn99 demo*
==> demo_typed.py <==
from __future__ import annotations

from typing import TypeVar

J = TypeVar("J", bound="JSON")
JSONObjectOf = dict[str, J]
JSON = str | JSONObjectOf["JSON"]
JSONObject = JSONObjectOf[JSON]

==> demo.py <==
from demo_typed import JSON
from demo_typed import JSONObjectOf


def identity(json: JSONObjectOf[JSON]):
    return json

==> demo0.py <==
import demo_typed


def identity0(json0: demo_typed.JSONObjectOf[demo_typed.JSON]):
    return json0

==> demo1.py <==
from demo_typed import JSON
from demo_typed import JSONObjectOf


def identity1(json1: JSONObjectOf[JSON]):
    return json1

==> demo2.py <==
from demo_typed import JSONObject


def identity2(json2: JSONObject):
    return json2

==> demo3.py <==
import demo_typed


def identity3(json3: demo_typed.JSONObject):
    return json3

expected behavior:

$ pyright demo0.py demo2.py | sed 's#'"$PWD/"'#./#g'
0 errors, 0 warnings, 0 informations

actual behavior:

$ pyright demo2.py demo0.py | sed 's#'"$PWD/"'#./#g'
./demo0.py
  ./demo0.py:4:5 - error: Return type, "dict[str, str | Unknown]", is partially unknown (reportUnknownParameterType)
  ./demo0.py:4:15 - error: Type of parameter "json0" is partially unknown
    Parameter type is "dict[str, str | Unknown]" (reportUnknownParameterType)
  ./demo0.py:4:46 - error: Type of "JSON" is partially unknown
    Type of "JSON" is "type[str] | Unknown" (reportUnknownMemberType)
  ./demo0.py:5:12 - error: Return type, "dict[str, str | Unknown]", is partially unknown (reportUnknownVariableType)
./demo2.py
  ./demo2.py:4:5 - error: Return type, "dict[str, str | Unknown]", is partially unknown (reportUnknownParameterType)
  ./demo2.py:4:15 - error: Type of parameter "json2" is partially unknown
    Parameter type is "dict[str, str | Unknown]" (reportUnknownParameterType)
  ./demo2.py:5:12 - error: Return type, "dict[str, str | Unknown]", is partially unknown (reportUnknownVariableType)
7 errors, 0 warnings, 0 informations

NOTE:

  • the only difference between the commands is the ordering of files.
  • the only difference between files is the details of how objects are imported.

VS Code extension or command-line

$ pyright --version
pyright 1.1.404

$ cat pyproject.toml
[tool.pyright]
reportUnknownParameterType = "error"

bukzor avatar Aug 26 '25 20:08 bukzor

Hmm, I'm not able to repro the problem as described. I don't see any errors regardless of the file order. Can you think of any other steps to repro?

Do you suspect that this is a regression? Are you able to repro it with older versions?

Does it require a particular pythonVersion?

erictraut avatar Aug 27 '25 05:08 erictraut

I could repro the problem as following:

Files

  • typ.py
Nested = list["StrOrNested"]
StrOrNested = str | Nested
Alias = Nested
  • a.py
# pyright: strict

from typ import Alias

def identity(x: Alias):
    return x

reveal_type(identity)
  • b.py
# pyright: strict

from typ import Nested

def identity(x: Nested):
    return x

reveal_type(identity)

Run

$ pyright a.py
a.py
  a.py:5:5 - error: Return type, "list[str | Unknown]", is partially unknown (reportUnknownParameterType)
  a.py:5:14 - error: Type of parameter "x" is partially unknown
    Parameter type is "list[str | Unknown]" (reportUnknownParameterType)
  a.py:6:12 - error: Return type, "list[str | Unknown]", is partially unknown (reportUnknownVariableType)
  a.py:8:13 - information: Type of "identity" is "(x: list[str | Unknown]) -> list[str | Unknown]"
3 errors, 0 warnings, 1 information
$ pyright b.py
b.py
  b.py:8:13 - information: Type of "identity" is "(x: list[str | Nested]) -> list[str | Nested]"
0 errors, 0 warnings, 1 information
$ pyright a.py b.py
a.py
  a.py:5:5 - error: Return type, "list[str | Unknown]", is partially unknown (reportUnknownParameterType)
  a.py:5:14 - error: Type of parameter "x" is partially unknown
    Parameter type is "list[str | Unknown]" (reportUnknownParameterType)
  a.py:6:12 - error: Return type, "list[str | Unknown]", is partially unknown (reportUnknownVariableType)
  a.py:8:13 - information: Type of "identity" is "(x: list[str | Unknown]) -> list[str | Unknown]"
b.py
  b.py:5:5 - error: Return type, "list[str | Unknown]", is partially unknown (reportUnknownParameterType)
  b.py:5:14 - error: Type of parameter "x" is partially unknown
    Parameter type is "list[str | Unknown]" (reportUnknownParameterType)
  b.py:6:12 - error: Return type, "list[str | Unknown]", is partially unknown (reportUnknownVariableType)
  b.py:8:13 - information: Type of "identity" is "(x: list[str | Unknown]) -> list[str | Unknown]"
6 errors, 0 warnings, 2 informations
$ pyright b.py a.py
a.py
  a.py:8:13 - information: Type of "identity" is "(x: list[str | list[StrOrNested]]) -> list[str | list[StrOrNested]]"
b.py
  b.py:8:13 - information: Type of "identity" is "(x: list[str | Nested]) -> list[str | Nested]"
0 errors, 0 warnings, 2 informations

Versions

$ python --version Python 3.12.3 $ pyright --version pyright 1.1.404

Workaround

use of new type alias syntax can avoid the problem:

Nested = list["StrOrNested"]
StrOrNested = str | Nested
type Alias = Nested

pinterior avatar Aug 27 '25 12:08 pinterior

Reproduces as described for me, in a new directory. I reproduced it with all python versions 3.8-3.13.

$ python --version 
Python 3.13.2

I did several spot-checks of pyright versions. This seems to be the transition point, but I didn't check exhaustively.

  • 1.1.343 -- repro
  • 1.1.342 -- no repro

bukzor avatar Aug 29 '25 23:08 bukzor

Thanks for the additional info. I was able to repro the problem using the example from @pinterior, and it was indeed a regression introduced in 1.1.343 (almost 8 months ago). This will be fixed in the next release.

erictraut avatar Aug 30 '25 22:08 erictraut

I'd love to know any of the details, if you have the time.

On Sat, Aug 30, 2025 at 5:40 PM Eric Traut @.***> wrote:

erictraut left a comment (microsoft/pyright#10850) https://github.com/microsoft/pyright/issues/10850#issuecomment-3239585982

Thanks for the additional info. I was able to repro the problem using the example from @pinterior https://github.com/pinterior, and it was indeed a regression introduced in 1.1.343 (almost 8 months ago). This will be fixed in the next release.

— Reply to this email directly, view it on GitHub https://github.com/microsoft/pyright/issues/10850#issuecomment-3239585982, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAE4KSDLMEVJQYWLPO7S7BD3QIR4VAVCNFSM6AAAAACE4EF2HCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTEMZZGU4DKOJYGI . You are receiving this because you authored the thread.Message ID: @.***>

bukzor avatar Aug 30 '25 22:08 bukzor

I noticed that the fix didn't include a regression test. Would you welcome a contribution?

On Sat, Aug 30, 2025 at 5:40 PM Buck Evan @.***> wrote:

I'd love to know any of the details, if you have the time.

On Sat, Aug 30, 2025 at 5:40 PM Eric Traut @.***> wrote:

erictraut left a comment (microsoft/pyright#10850) https://github.com/microsoft/pyright/issues/10850#issuecomment-3239585982

Thanks for the additional info. I was able to repro the problem using the example from @pinterior https://github.com/pinterior, and it was indeed a regression introduced in 1.1.343 (almost 8 months ago). This will be fixed in the next release.

— Reply to this email directly, view it on GitHub https://github.com/microsoft/pyright/issues/10850#issuecomment-3239585982, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAE4KSDLMEVJQYWLPO7S7BD3QIR4VAVCNFSM6AAAAACE4EF2HCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTEMZZGU4DKOJYGI . You are receiving this because you authored the thread.Message ID: @.***>

bukzor avatar Sep 02 '25 14:09 bukzor

This is addressed in pyright 1.1.405

erictraut avatar Sep 03 '25 18:09 erictraut