Skip to content

Type checking problems with pyright #9

@wgmo

Description

@wgmo

The attached code demonstrates a type checking problem which occurs when constructing operations with graphtik at the head of its master branch, which is not present in the 10.5.0 release!

In summary, the following two type errors are found by pyright:

  • Upon attempting to compose an operation which has been created using the @operation decorator, the error Argument of type "dict[Unknown, Unknown]" cannot be assigned to parameter "op1" of type "Operation" in function "compose" is triggered.
  • Upon attempting to pass a string as the name of an operation being created using the @operation decorator, the error Argument of type "Literal['astronaut_responder']" cannot be assigned to parameter "name" of type "Token" in function "operation" is triggered.

Is this likely to be a misconfiguration somewhere on my side, or is there something which is a work-in-progress at the head of master branch which should be ignored for now?

from graphtik.fnop import operation
from graphtik.pipeline import compose


@operation(needs="needed", provides="provided")
def type_checker_not_ok_op(s: str) -> str:
    """Provoke a type checking failure by using the decorator to create a `FnOp`."""
    return "hello " + s


def type_checker_ok_fn(s: str) -> str:
    """Bare undecorated function to wrap as an operation."""
    return "hello " + s


type_checker_ok_op = operation(type_checker_ok_fn, needs="needed", provides="provided")


@operation(name="astronaut_responder", needs="astronaut_name", provides="provided")
def type_checker_not_ok_name_op(astronaut_name: str) -> str:
    """Provoke a type checking failure for the `name` parameter using the decorator."""
    return "I'm sorry, " + astronaut_name + ", I'm afraid I can't do that."


def type_checker_checker_activator() -> None:
    """Provoke the type checker."""
    pipe_1 = compose("A pipeline", type_checker_not_ok_op)
    pipe_2 = compose("Another pipeline", type_checker_ok_op)
    pipe_3 = compose("A third pipeline", type_checker_not_ok_name_op)

    # All pipelines work, but:
    # * the first one fails type checking:

    # * the second one is identical and passes type checking

    # * the third one fails type checking for the `name` parameter:

    # $ pyright --version
    # pyright 1.1.323
    # $ pyright foo.py
    # /home/williamg/Projects/foo.py
    #   /home/williamg/Projects/foo.py:19:17 - error: Argument of type "Literal['astronaut_responder']" cannot be assigned to parameter "name" of type "Token" in function "operation"
    #     "Literal['astronaut_responder']" is incompatible with "Token" (reportGeneralTypeIssues)
    #   /home/williamg/Projects/foo.py:27:36 - error: Argument of type "dict[Unknown, Unknown]" cannot be assigned to parameter "op1" of type "Operation" in function "compose"
    #     "dict[Unknown, Unknown]" is incompatible with "Operation" (reportGeneralTypeIssues)
    #   /home/williamg/Projects/foo.py:29:42 - error: Argument of type "dict[Unknown, Unknown]" cannot be assigned to parameter "op1" of type "Operation" in function "compose"
    #     "dict[Unknown, Unknown]" is incompatible with "Operation" (reportGeneralTypeIssues)
    # 3 errors, 0 warnings, 0 informations

    print(pipe_1.compute({"needed": "cruel world"})["provided"])
    print(pipe_2.compute({"needed": "kind world"})["provided"])
    print(pipe_3.compute({"astronaut_name": "Dave"})["provided"])


if __name__ == "__main__":
    type_checker_checker_activator()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions