Skip to content

TypeGuard applied outside of it's scope #2852

@ikamensh

Description

@ikamensh

Hello, below is a suspected bug / inconsistent behavior I couldn't understand.

In the code below, a value is passed through to a submethod with same typing annotation, but an error is generated.

Code:

    def __init__(
        self,
        low: Union[SupportsFloat, np.ndarray],
       ...
    ):
        if shape is not None:
            shape = tuple(shape)
        elif not np.isscalar(low):
            shape = low.shape  # type: ignore
        elif not np.isscalar(high):
            shape = high.shape  # type: ignore
        else:
            raise ValueError(
                "shape must be provided or inferred from the shapes of low or high"
            )
        low = _broadcast(low, dtype, shape, inf_sign="-")  # error


def _broadcast(
    value: Union[SupportsFloat, np.ndarray],
    ...
) -> np.ndarray:
        ...

Error:

/home/runner/work/gym/gym/gym/spaces/box.py
  /home/runner/work/gym/gym/gym/spaces/box.py:53:32 - error: Argument of type "SupportsFloat | ndarray[Unknown, Unknown] | generic | bool | int | float | complex | str | bytes | memoryview" cannot be assigned to parameter "value" of type "SupportsFloat | ndarray[Unknown, Unknown]" in function "_broadcast"
    Type "SupportsFloat | ndarray[Unknown, Unknown] | generic | bool | int | float | complex | str | bytes | memoryview" cannot be assigned to type "SupportsFloat | ndarray[Unknown, Unknown]"
      Type "generic" cannot be assigned to type "SupportsFloat | ndarray[Unknown, Unknown]"
        "generic" is incompatible with protocol "SupportsFloat"
          "__float__" is not present
        "generic" is incompatible with "ndarray[Unknown, Unknown]"
      Type "complex" cannot be assigned to type "SupportsFloat | ndarray[Unknown, Unknown]"
        "complex" is incompatible with protocol "SupportsFloat"
          "__float__" is not present (reportGeneralTypeIssues)
1 error, 0 warnings, 0 infos 

CI:

Run jakebailey/pyright-action@v1
  with:
    version: 1.1.204
    python-platform: Linux
    python-version: 3.7
    no-comments: true
    lib: false
    warnings: false
  env:
    PYRIGHT_VERSION: 1.1.204
    pythonLocation: /opt/hostedtoolcache/Python/3.10.1/x64
    LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.10.1/x64/lib

Complete source code: https://github.com/boschresearch/gym/blob/df82bdc27bc877bcaf98aecb203bc07fbea19ceb/gym/spaces/box.py#L53

Full CI log:
https://github.com/openai/gym/runs/4789425067?check_suite_focus=true

Metadata

Metadata

Assignees

No one assigned

    Labels

    as designedNot a bug, working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions