Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem in using mypy
#1
Hi
I installed on the Python mypy library.
I save my files on D drive in a folder.
In cmd, I changed the directory to the folder. Then I wrote python word and the prompt changed to >>>.
then I wrote import mypy
In that folder, there is a file with the name example.py (I am reading the page https://www.geeksforgeeks.org/function-a...ns-python/ and this snippet code belongs to it. The accuracy of the code is not important and this page teaches annotation in Python and the code must create an error):
# String slicing function that returns a string from start index to end index.
def slice(string:str, start: int, end: int) -> str:
	return string[start:end]

slice([1, 2, 3, 4, 5], 2, 4)
Now, when I write in cmd mypy example.py
it takes an error and shows:
Error:
>>> mypy example.py File "<stdin>", line 1 mypy example1.py ^^^^^^^^ SyntaxError: invalid syntax
according to the site, it must be shown:
exampl.py:4 error: argument 1 to "splice" has incompatible type list[int]; expected "str"

I tried several times, but the problem is there.
Can you guide me about it?
thanks
Reply
#2
mypy is a static analysis tool, not a python package. You run mypy from the command line, not from python. Open up a terminal, change directories to your python code folder and type mypy example.py. It will display this:

example.py:5: error: Argument 1 to "slice" has incompatible type "list[int]"; expected "str" [arg-type]
Found 1 error in 1 file (checked 1 source file)
Reply
#3
(Sep-03-2023, 03:25 PM)deanhystad Wrote: mypy is a static analysis tool, not a python package. You run mypy from the command line, not from python. Open up a terminal, change directories to your python code folder and type mypy example.py. It will display this:

example.py:5: error: Argument 1 to "slice" has incompatible type "list[int]"; expected "str" [arg-type]
Found 1 error in 1 file (checked 1 source file)

hi
I did as you said, Below is what is on cmd( I wanted to use mypy with example1.py file):
Error:
C:\Users\akbar>cd /d D:\14020412_enteghal\akb_python\akb_py_projects\ D:\14020412_enteghal\akb_python\akb_py_projects>mypy example1.py D:\14020412_enteghal\akb_python\akb_py_projects\__init__.py: error: INTERNAL ERROR -- Please try using mypy master on Gi tHub: https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build If this issue continues with mypy master, please report a bug at https://github.com/python/mypy/issues version: 1.7.0+dev D:\14020412_enteghal\akb_python\akb_py_projects\__init__.py: : note: please use --show-traceback to print a traceback wh en reporting a bug D:\14020412_enteghal\akb_python\akb_py_projects>
what do I do now?
Reply
#4
You should use mypy example1.py --show-traceback to look what log say.

Here is run and with upgrade of mypy.
G:\div_code
λ python -V
Python 3.11.3

G:\div_code
λ pip -V
pip 23.1.2 from C:\python311\Lib\site-packages\pip (python 3.11)

# Upgrade mypy 
G:\div_code
λ pip install mypy --upgrade --no-cache-dir --force-reinstall
Collecting mypy
  Downloading mypy-1.5.1-cp311-cp311-win_amd64.whl (8.9 MB)
     ---------------------------------------- 8.9/8.9 MB 6.4 MB/s eta 0:00:00
Collecting typing-extensions>=4.1.0 (from mypy)
  Downloading typing_extensions-4.7.1-py3-none-any.whl (33 kB)
Collecting mypy-extensions>=1.0.0 (from mypy)
  Downloading mypy_extensions-1.0.0-py3-none-any.whl (4.7 kB)
Installing collected packages: typing-extensions, mypy-extensions, mypy
  Attempting uninstall: typing-extensions
    Found existing installation: typing_extensions 4.6.1
    Uninstalling typing_extensions-4.6.1:
      Successfully uninstalled typing_extensions-4.6.1
  Attempting uninstall: mypy-extensions
    Found existing installation: mypy-extensions 1.0.0
    Uninstalling mypy-extensions-1.0.0:
      Successfully uninstalled mypy-extensions-1.0.0
  Attempting uninstall: mypy
    Found existing installation: mypy 1.5.1
    Uninstalling mypy-1.5.1:
      Successfully uninstalled mypy-1.5.1
Successfully installed mypy-1.5.1 mypy-extensions-1.0.0 typing-extensions-4.7.1

# Test it
G:\div_code
λ mypy --version
mypy 1.5.1 (compiled: yes)

G:\div_code
λ mypy slice_test.py
slice_test.py:5: error: Argument 1 to "slice" has incompatible type "list[int]"; expected "str"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

# Fix it 
G:\div_code
λ mypy slice_test.py
Success: no issues found in 1 source file

# Code is now
G:\div_code
λ rich slice_test.py
# String slicing function that returns a string from start index to end index.
def slice(string:str, start: int, end: int) -> str:
    return string[start:end]

slice('1, 2, 3, 4, 5', 2, 4)
Reply
#5
hi
I did as below but I am challenged with it until now:
I installed git, so in my pc I have git cmd, git bash, git gui.
In git cmd: I changed the directory to my python's files folder, namely: D:\14020412_enteghal\akb_python\akb_py_projects
then according to address:https://mypy.readthedocs.io/en/stable/co...mypy-build, I wrote in git cmd below commands:
git clone https://github.com/python/mypy.git
cd mypy
python -m pip install --upgrade mypy
then again when I wrote mypy example1.py it took an error. Also for test, I copied example1.py in folder mypy and again when in git cmd I wrote mypy example1.py, it took the error.
Below is what I saw in git cmd now:

Error:
C:\Users\akbar>cd/d D:\14020412_enteghal\akb_python\akb_py_projects D:\14020412_enteghal\akb_python\akb_py_projects>git clone https://github.com/python/mypy.git Cloning into 'mypy'... remote: Enumerating objects: 91436, done. remote: Counting objects: 100% (1084/1084), done. remote: Compressing objects: 100% (656/656), done. remote: Total 91436 (delta 641), reused 758 (delta 425), pack-reused 90352Receiving objects: 100% (91436/91436), 62.00 M Resolving deltas: 100% (70447/70447), done. Updating files: 100% (1434/1434), done. D:\14020412_enteghal\akb_python\akb_py_projects>cd mypy D:\14020412_enteghal\akb_python\akb_py_projects\mypy>python3 -m pip install --upgrade Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases. D:\14020412_enteghal\akb_python\akb_py_projects\mypy>python -m pip install --upgrade ERROR: You must give at least one requirement to install (see "pip help install") D:\14020412_enteghal\akb_python\akb_py_projects\mypy>python -m pip install --upgrade mypy Requirement already satisfied: mypy in c:\users\akbar\appdata\local\programs\python\python311\lib\site-packages (1.7.0+d ev) Requirement already satisfied: typing-extensions>=4.1.0 in c:\users\akbar\appdata\local\programs\python\python311\lib\si te-packages (from mypy) (4.7.1) Requirement already satisfied: mypy-extensions>=1.0.0 in c:\users\akbar\appdata\local\programs\python\python311\lib\site -packages (from mypy) (1.0.0) D:\14020412_enteghal\akb_python\akb_py_projects\mypy> mypy example1.py mypy: can't read file 'example1.py': No such file or directory D:\14020412_enteghal\akb_python\akb_py_projects\mypy>cd.. D:\14020412_enteghal\akb_python\akb_py_projects>mypy example1.py D:\14020412_enteghal\akb_python\akb_py_projects\__init__.py: error: INTERNAL ERROR -- Please try using mypy master on Gi tHub: https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build If this issue continues with mypy master, please report a bug at https://github.com/python/mypy/issues version: 1.7.0+dev D:\14020412_enteghal\akb_python\akb_py_projects\__init__.py: : note: please use --show-traceback to print a traceback wh en reporting a bug D:\14020412_enteghal\akb_python\akb_py_projects>cd mypy D:\14020412_enteghal\akb_python\akb_py_projects\mypy>mypy example1.py D:\14020412_enteghal\akb_python\akb_py_projects\__init__.py: error: INTERNAL ERROR -- Please try using mypy master on Gi tHub: https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build If this issue continues with mypy master, please report a bug at https://github.com/python/mypy/issues version: 1.7.0+dev D:\14020412_enteghal\akb_python\akb_py_projects\__init__.py: : note: please use --show-traceback to print a traceback wh en reporting a bug D:\14020412_enteghal\akb_python\akb_py_projects\mypy>
can anyone help me with this problem?
thanks
Reply
#6
(Sep-04-2023, 11:47 AM)snippsat Wrote: You should use mypy example1.py --show-traceback to look what log say.

Here is run and with upgrade of mypy.
G:\div_code
λ python -V
Python 3.11.3

G:\div_code
λ pip -V
pip 23.1.2 from C:\python311\Lib\site-packages\pip (python 3.11)

# Upgrade mypy 
G:\div_code
λ pip install mypy --upgrade --no-cache-dir --force-reinstall
Collecting mypy
  Downloading mypy-1.5.1-cp311-cp311-win_amd64.whl (8.9 MB)
     ---------------------------------------- 8.9/8.9 MB 6.4 MB/s eta 0:00:00
Collecting typing-extensions>=4.1.0 (from mypy)
  Downloading typing_extensions-4.7.1-py3-none-any.whl (33 kB)
Collecting mypy-extensions>=1.0.0 (from mypy)
  Downloading mypy_extensions-1.0.0-py3-none-any.whl (4.7 kB)
Installing collected packages: typing-extensions, mypy-extensions, mypy
  Attempting uninstall: typing-extensions
    Found existing installation: typing_extensions 4.6.1
    Uninstalling typing_extensions-4.6.1:
      Successfully uninstalled typing_extensions-4.6.1
  Attempting uninstall: mypy-extensions
    Found existing installation: mypy-extensions 1.0.0
    Uninstalling mypy-extensions-1.0.0:
      Successfully uninstalled mypy-extensions-1.0.0
  Attempting uninstall: mypy
    Found existing installation: mypy 1.5.1
    Uninstalling mypy-1.5.1:
      Successfully uninstalled mypy-1.5.1
Successfully installed mypy-1.5.1 mypy-extensions-1.0.0 typing-extensions-4.7.1

# Test it
G:\div_code
λ mypy --version
mypy 1.5.1 (compiled: yes)

G:\div_code
λ mypy slice_test.py
slice_test.py:5: error: Argument 1 to "slice" has incompatible type "list[int]"; expected "str"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

# Fix it 
G:\div_code
λ mypy slice_test.py
Success: no issues found in 1 source file

# Code is now
G:\div_code
λ rich slice_test.py
# String slicing function that returns a string from start index to end index.
def slice(string:str, start: int, end: int) -> str:
    return string[start:end]

slice('1, 2, 3, 4, 5', 2, 4)


hi snippsat
after doing what I explained in my last reply when I go to mypy folder in cmd or git cmd and type mypy --version, the below is appeared:
Error:
D:\14020412_enteghal\akb_python\akb_py_projects>cd mypy D:\14020412_enteghal\akb_python\akb_py_projects\mypy>mypy --version mypy 1.7.0+dev (compiled: no)
is mypy installed correctly on my PC?
if your answer is yes, how can I use mypy with example1.py? must I copy it to mypy folder?
thanks again
Reply
#7
hi
what I did:
Error:
D:\14020412_enteghal\akb_python\akb_py_projects>python -m pip install mypy --upgrade Requirement already satisfied: mypy in c:\users\akbar\appdata\local\programs\python\python311\lib\site-packages (1.7.0+d ev) Requirement already satisfied: typing-extensions>=4.1.0 in c:\users\akbar\appdata\local\programs\python\python311\lib\si te-packages (from mypy) (4.7.1) Requirement already satisfied: mypy-extensions>=1.0.0 in c:\users\akbar\appdata\local\programs\python\python311\lib\site -packages (from mypy) (1.0.0) D:\14020412_enteghal\akb_python\akb_py_projects>python -m pip install mypy --upgrade --no-cache-dir --force-reinstall Collecting mypy Obtaining dependency information for mypy from https://files.pythonhosted.org/packages/4e/11/ac861ca5d9b16fd5b781c1941 254d4e382e8eaab90e11f41f193d9222b7e/mypy-1.5.1-cp311-cp311-win_amd64.whl.metadata Downloading mypy-1.5.1-cp311-cp311-win_amd64.whl.metadata (1.8 kB) Collecting typing-extensions>=4.1.0 (from mypy) Obtaining dependency information for typing-extensions>=4.1.0 from https://files.pythonhosted.org/packages/ec/6b/63cc3 df74987c36fe26157ee12e09e8f9db4de771e0f3404263117e75b95/typing_extensions-4.7.1-py3-none-any.whl.metadata Downloading typing_extensions-4.7.1-py3-none-any.whl.metadata (3.1 kB) Collecting mypy-extensions>=1.0.0 (from mypy) Downloading mypy_extensions-1.0.0-py3-none-any.whl (4.7 kB) Downloading mypy-1.5.1-cp311-cp311-win_amd64.whl (8.9 MB) ---------------------------------------- 8.9/8.9 MB 828.3 kB/s eta 0:00:00 Downloading typing_extensions-4.7.1-py3-none-any.whl (33 kB) Installing collected packages: typing-extensions, mypy-extensions, mypy Attempting uninstall: typing-extensions Found existing installation: typing_extensions 4.7.1 Uninstalling typing_extensions-4.7.1: Successfully uninstalled typing_extensions-4.7.1 Attempting uninstall: mypy-extensions Found existing installation: mypy-extensions 1.0.0 Uninstalling mypy-extensions-1.0.0: Successfully uninstalled mypy-extensions-1.0.0 Attempting uninstall: mypy Found existing installation: mypy 1.7.0+dev Uninstalling mypy-1.7.0+dev: Successfully uninstalled mypy-1.7.0+dev Successfully installed mypy-1.5.1 mypy-extensions-1.0.0 typing-extensions-4.7.1 D:\14020412_enteghal\akb_python\akb_py_projects>mypy example1.py D:\14020412_enteghal\akb_python\akb_py_projects\__init__.py: error: INTERNAL ERROR -- Please try using mypy master on Gi tHub: https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build If this issue continues with mypy master, please report a bug at https://github.com/python/mypy/issues version: 1.5.1 D:\14020412_enteghal\akb_python\akb_py_projects\__init__.py: : note: please use --show-traceback to print a traceback wh en reporting a bug D:\14020412_enteghal\akb_python\akb_py_projects>cd mypy D:\14020412_enteghal\akb_python\akb_py_projects\mypy>mypy example1.py D:\14020412_enteghal\akb_python\akb_py_projects\__init__.py: error: INTERNAL ERROR -- Please try using mypy master on Gi tHub: https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build If this issue continues with mypy master, please report a bug at https://github.com/python/mypy/issues version: 1.5.1 D:\14020412_enteghal\akb_python\akb_py_projects\__init__.py: : note: please use --show-traceback to print a traceback wh en reporting a bug D:\14020412_enteghal\akb_python\akb_py_projects\mypy>mypy example1.py --show-traceback D:\14020412_enteghal\akb_python\akb_py_projects\__init__.py: error: INTERNAL ERROR -- Please try using mypy master on Gi tHub: https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build Please report a bug at https://github.com/python/mypy/issues version: 1.5.1 Traceback (most recent call last): File "<frozen runpy>", line 198, in _run_module_as_main File "<frozen runpy>", line 88, in _run_code File "C:\Users\akbar\AppData\Local\Programs\Python\Python311\Scripts\mypy.exe\__main__.py", line 7, in <module> sys.exit(console_entry()) File "mypy\build.py", line 2069, in wrap_context self.path, File "mypy\build.py", line 2176, in parse_file ) File "mypy\build.py", line 841, in parse_file parse_time=time.time() - t0, File "mypy\parse.py", line 22, in parse return mypy.fastparse.parse(source, fnam=fnam, module=module, errors=errors, options=options) File "mypy\fastparse.py", line 219, in parse assert options.python_version[0] >= 3 File "mypy\fastparse.py", line 134, in ast3_parse ) -> AST: File "C:\Users\akbar\AppData\Local\Programs\Python\Python311\Lib\ast.py", line 50, in parse return compile(source, filename, mode, flags, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ValueError: source code string cannot contain null bytes D:\14020412_enteghal\akb_python\akb_py_projects\__init__.py: : note: use --pdb to drop into pdb D:\14020412_enteghal\akb_python\akb_py_projects\mypy>
Reply
#8
(Sep-04-2023, 12:20 PM)akbarza Wrote: is mypy installed correctly on my PC?
Now you using development version 1.7.0+dev,you should use the stable 1.5.1 version.
Also mypy should work from any folder in cmd,as you install to your Python version that should be in Os Path.
Test in cmd.
C:\>python  -V
Python 3.11.3

C:\>python -V
Python 3.11.3

C:\>pip -V
pip 23.1.2 from C:\python311\Lib\site-packages\pip (python 3.11)

C:\>mypy --version
mypy 1.5.1 (compiled: yes)
Do this test make a virtual environment,then install mypy.
# Make 
c:\code>python -m venv new_env

# Cd in
c:\code>cd new_env

# Activate
c:\code\new_env>c:\code\new_env\Scripts\activate

# install
(new_env) c:\code\new_env>pip install mypy --no-cache-dir
Collecting mypy
  Downloading mypy-1.5.1-cp311-cp311-win_amd64.whl (8.9 MB)
     ---------------------------------------- 8.9/8.9 MB 7.6 MB/s eta 0:00:00
Collecting typing-extensions>=4.1.0
  Downloading typing_extensions-4.7.1-py3-none-any.whl (33 kB)
Collecting mypy-extensions>=1.0.0
  Downloading mypy_extensions-1.0.0-py3-none-any.whl (4.7 kB)
Installing collected packages: typing-extensions, mypy-extensions, mypy
Successfully installed mypy-1.5.1 mypy-extensions-1.0.0 typing-extensions-4.7.1

# Test
(new_env) c:\code\new_env>mypy --version
mypy 1.5.1 (compiled: yes)
So if work in virtual environment(it's like new Python version),then is the problem with your install on OS python version.
Then do as i advice pip install mypy --upgrade --no-cache-dir --force-reinstall
Reply
#9
hi snppsat
i replied to you, but my net disconnected and my reply was killed.
good news: i did as you said step by step and I reached to:

Output:
(new_env) c:\code\new_env>mypy --version mypy 1.5.1 (compiled: yes)
also i copied example1 in C:\code\new_env and then :

C:\WINDOWS\system32>cd C:\code\new_env

Output:
C:\code\new_env>mypy example1.py example1.py:5: ←[1m←[91merror:←[0m Argument 1 to ←[0m←[1m"slice"←[0m has incompatible type ←[0m←[1m"list[int]"←[0m; expe cted ←[0m←[1m"str"←[0m ←[0m←[93m[arg-type]←[0m ←[1m←[91mFound 1 error in 1 file (checked 1 source file)←[0m C:\code\new_env>
thanks
Reply
#10
hi snppsat
i replied to you, but my net disconnected and my reply was killed.
good news: i did as you said step by step and I reached to:

Output:
(new_env) c:\code\new_env>mypy --version mypy 1.5.1 (compiled: yes)
also i copied example1 in C:\code\new_env and then :

C:\WINDOWS\system32>cd C:\code\new_env

Output:
C:\code\new_env>mypy example1.py example1.py:5: ←[1m←[91merror:←[0m Argument 1 to ←[0m←[1m"slice"←[0m has incompatible type ←[0m←[1m"list[int]"←[0m; expe cted ←[0m←[1m"str"←[0m ←[0m←[93m[arg-type]←[0m ←[1m←[91mFound 1 error in 1 file (checked 1 source file)←[0m C:\code\new_env>
thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  mypy, type aliases and type variables tomciodev 1 717 Oct-18-2023, 10:08 AM
Last Post: Larz60+
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 493 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Mypy typing error WannaBePythonDev 2 1,940 Sep-05-2021, 10:18 AM
Last Post: WannaBePythonDev

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020