Python-Type-Challenges icon indicating copy to clipboard operation
Python-Type-Challenges copied to clipboard

[Bug] Pass challenges with `raise`

Open F-park opened this issue 1 year ago • 6 comments

Bug description

image

If # expect-type-error not in test_code, challenge can pass with raise.

Challenges

  • advanced-forward
  • advanced-never2

F-park avatar Jan 18 '24 03:01 F-park

Yes, this is a known issue. I haven't thought of good ways to solve it

laike9m avatar Jan 18 '24 06:01 laike9m

Took another look. "advanced-forward" should be easy to fix, but never2 is hard, as I haven't found a way to force pyright to fail..

laike9m avatar Jan 18 '24 06:01 laike9m

If we can insert some imports before user_code and add "reportUnusedImport": true to pyrightconfig.json, it is easy to solve.


advanced-forward need to force user to annotate return_type for function

# advanced-forward-fix
## End of your code ##
+# pyright: analyzeUnannotatedFunctions=false
from typing import assert_type

inst = MyClass(x=1)
assert_type(inst.copy(), MyClass)

F-park avatar Jan 18 '24 10:01 F-park

Actually, I find that pyright will ignore whatever code following raise, since it thinks that those code is unreachable.

Example

So it really seems to have no solution..

laike9m avatar Jan 19 '24 06:01 laike9m

So it really seems to have no solution..

image

mypy can detect the name-defined error although code is unreachable, I think we can mix mypy with pyright.


If we add the below to pyproject.toml, mypy will show test.py:4: error: Statement is unreachable [unreachable]

[tool.mypy]
warn_unreachable = true

F-park avatar Jan 19 '24 06:01 F-park

Using sourcery is a no-go, using mypy maybe could be an option but certainly would complicate things. Another option is to create a special rule just for raise and warn users about it. I'll leave this one open and think about it.

laike9m avatar Jan 19 '24 16:01 laike9m