Anonymous
Mypy не может найти ошибку типа в простой однострочной функции [дубликат]
Сообщение
Anonymous » 17 июл 2024, 09:39
Я написал очень простой модуль Python с несовместимыми подсказками типов, и mypy до сих пор не находит ошибок.
Вот модуль
а вот полный сеанс терминала, иллюстрирующий неожиданное поведение:
Код: Выделить всё
~/src$ mkdir temp
~/src$ cd temp
~/src/temp$ echo -e "def func(x: int) -> float:\n return x"
def func(x: int) -> float:
return x
~/src/temp$ echo -e "def func(x: int) -> float:\n return x" > mymodule.py
~/src/temp$ mkvirtualenv temp
created virtual environment CPython3.9.7.final.0-64 in 99ms
creator CPython3Posix(dest=/home/danielsank/.virtualenvs/temp, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/danielsank/.local/share/virtualenv)
added seed packages: pip==20.3.4, pkg_resources==0.0.0, setuptools==44.1.1, wheel==0.34.2
activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
virtualenvwrapper.user_scripts creating /home/danielsank/.virtualenvs/temp/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/danielsank/.virtualenvs/temp/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/danielsank/.virtualenvs/temp/bin/preactivate
virtualenvwrapper.user_scripts creating /home/danielsank/.virtualenvs/temp/bin/postactivate
virtualenvwrapper.user_scripts creating /home/danielsank/.virtualenvs/temp/bin/get_env_details
(temp) ~/src/temp$ pip install mypy
Collecting mypy
Using cached mypy-0.910-cp39-cp39-manylinux2010_x86_64.whl (23.2 MB)
Collecting toml
Using cached toml-0.10.2-py2.py3-none-any.whl (16 kB)
Collecting mypy-extensions=0.4.3
Using cached mypy_extensions-0.4.3-py2.py3-none-any.whl (4.5 kB)
Collecting typing-extensions>=3.7.4
Using cached typing_extensions-3.10.0.2-py3-none-any.whl (26 kB)
Installing collected packages: typing-extensions, toml, mypy-extensions, mypy
Successfully installed mypy-0.910 mypy-extensions-0.4.3 toml-0.10.2 typing-extensions-3.10.0.2
(temp) ~/src/temp$ mypy mymodule.py
Success: no issues found in 1 source file
Почему mypy не замечает, что подсказки типов func несовместимы с его кодом?
Подробнее здесь:
https://stackoverflow.com/questions/695 ... e-function
1721198357
Anonymous
Я написал очень простой модуль Python с несовместимыми подсказками типов, и mypy до сих пор не находит ошибок. Вот модуль [code]def func(x: int) -> float: return x [/code] а вот полный сеанс терминала, иллюстрирующий неожиданное поведение: [code]~/src$ mkdir temp ~/src$ cd temp ~/src/temp$ echo -e "def func(x: int) -> float:\n return x" def func(x: int) -> float: return x ~/src/temp$ echo -e "def func(x: int) -> float:\n return x" > mymodule.py ~/src/temp$ mkvirtualenv temp created virtual environment CPython3.9.7.final.0-64 in 99ms creator CPython3Posix(dest=/home/danielsank/.virtualenvs/temp, clear=False, no_vcs_ignore=False, global=False) seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/danielsank/.local/share/virtualenv) added seed packages: pip==20.3.4, pkg_resources==0.0.0, setuptools==44.1.1, wheel==0.34.2 activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator virtualenvwrapper.user_scripts creating /home/danielsank/.virtualenvs/temp/bin/predeactivate virtualenvwrapper.user_scripts creating /home/danielsank/.virtualenvs/temp/bin/postdeactivate virtualenvwrapper.user_scripts creating /home/danielsank/.virtualenvs/temp/bin/preactivate virtualenvwrapper.user_scripts creating /home/danielsank/.virtualenvs/temp/bin/postactivate virtualenvwrapper.user_scripts creating /home/danielsank/.virtualenvs/temp/bin/get_env_details (temp) ~/src/temp$ pip install mypy Collecting mypy Using cached mypy-0.910-cp39-cp39-manylinux2010_x86_64.whl (23.2 MB) Collecting toml Using cached toml-0.10.2-py2.py3-none-any.whl (16 kB) Collecting mypy-extensions=0.4.3 Using cached mypy_extensions-0.4.3-py2.py3-none-any.whl (4.5 kB) Collecting typing-extensions>=3.7.4 Using cached typing_extensions-3.10.0.2-py3-none-any.whl (26 kB) Installing collected packages: typing-extensions, toml, mypy-extensions, mypy Successfully installed mypy-0.910 mypy-extensions-0.4.3 toml-0.10.2 typing-extensions-3.10.0.2 (temp) ~/src/temp$ mypy mymodule.py Success: no issues found in 1 source file [/code] Почему mypy не замечает, что подсказки типов func несовместимы с его кодом? Подробнее здесь: [url]https://stackoverflow.com/questions/69505100/mypy-fails-to-find-a-type-error-in-simple-one-line-function[/url]