mypy now reports type errors after upgrading from sympy 1.9 to 1.10.1

28 views
Skip to first unread message

Arthur Ryman

unread,
May 17, 2022, 2:54:50 PM5/17/22
to sympy
I used type hints in my sympy 1.9 application and found no type error when running mypy 0.950.

I upgraded to sympy 1.10.1 and now mypy reports lots of errors.

For example:

error: Variable "sympy.matrices.Matrix" is not valid as a type

Does anyone know what changed in 1.10.1?

Are there examples of the correct way to use type hints with sympy 1.10.1?

Thanks.

-- Arthur Ryman

Qijia Liu

unread,
May 17, 2022, 10:46:34 PM5/17/22
to sympy
SymPy becomes PEP 561 compatible since 1.10
As a side effect, since SymPy is not well typed in many places, it causes false positives at downstream.

Arthur Ryman

unread,
May 18, 2022, 11:57:35 AM5/18/22
to sympy
Thanks. That explains the change in behavior. I looked at the PR and it appears to simply add an empty py.types file. Are there any type information stubs (.pyi files)?

Do you know of any examples of how to use type hints to indicate, e.g., Matrix args or results?

-- Arthur

Qijia Liu

unread,
May 18, 2022, 6:57:25 PM5/18/22
to sympy
No, currently there are no .pyi files in SymPy.

Could you please submit issues with code snippets that mypy reports error?

One workaround is to let mypy skip SymPy, just like pre-1.9 behavior. Reference: https://github.com/python/mypy/issues/7769
Here is an example that contains both error-typed numpy and sympy code:

import numpy
import sympy

nv: int = numpy.__version__
x: int = sympy.Symbol('x')

If you run mypy with it, you will get 2 errors.
Then if you create a mypy.ini as below and run mypy again, you will only get the numpy error

[mypy-sympy.*]
follow_imports = skip
follow_imports_for_stubs = True
Reply all
Reply to author
Forward
0 new messages