Skip to content

Instantly share code, notes, and snippets.

@bukzor
Last active July 19, 2022 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bukzor/c11517f8a5c38b62cab157d25492caea to your computer and use it in GitHub Desktop.
Save bukzor/c11517f8a5c38b62cab157d25492caea to your computer and use it in GitHub Desktop.
$ pyright mycode_test.py
pyright 1.1.261
./mycode_test.py
  ./mycode_test.py:42:16 - error: Cannot assign member "slow_method" for type "Gizmo"
    Expression of type "() -> Literal[3]" cannot be assigned to member "slow_method" of class "Gizmo"
      Type "() -> Literal[3]" cannot be assigned to type "() -> Frob"
        Function return type "Literal[3]" is incompatible with type "Frob"
          "Literal[3]" is incompatible with "Frob" (reportGeneralTypeIssues)
1 error, 0 warnings, 0 informations 
Completed in 0.564sec
exit code: 1
from __future__ import annotations
def thing_to_test(gizmo: Gizmo):
return gizmo.slow_method()
class Frob:
pass
class Gizmo:
def slow_method(self):
return Frob()
import typing
from unittest.mock import Mock
import mycode
def fake_slow_method():
return mycode.Frob()
def fake_slow_method_wrong():
return 3
def test_a_thing():
mocked_gizmo = typing.cast(mycode.Gizmo, Mock())
mocked_gizmo.slow_method = fake_slow_method
assert mycode.thing_to_test(mocked_gizmo) == 3
def test_a_thing_wrong():
mocked_gizmo = typing.cast(mycode.Gizmo, Mock())
mocked_gizmo.slow_method = fake_slow_method_wrong
assert mycode.thing_to_test(mocked_gizmo) == 3
[tool.pyright]
include = ["**"]
typeCheckingMode = "strict"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment