Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression: crash in initialize_unix_colors in 0.920 #11768

Closed
mjog opened this issue Dec 16, 2021 · 10 comments · Fixed by python/typeshed#6620
Closed

Regression: crash in initialize_unix_colors in 0.920 #11768

mjog opened this issue Dec 16, 2021 · 10 comments · Fixed by python/typeshed#6620
Labels

Comments

@mjog
Copy link

mjog commented Dec 16, 2021

Bug Report

As of 0.920, running mypy as an emacs compile command raises the following error:

-*- mode: compilation; default-directory: "~/Projects/vee.net/planet-srv/" -*-
Compilation started at Thu Dec 16 21:35:25

cd $SRC_ROOT && poetry run mypy
Traceback (most recent call last):
  File "/home/mjog/.cache/pypoetry/virtualenvs/planet-srv-HUCGNwqs-py3.9/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/home/mjog/.cache/pypoetry/virtualenvs/planet-srv-HUCGNwqs-py3.9/lib/python3.9/site-packages/mypy/__main__.py", line 11, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "mypy/main.py", line 70, in main
  File "mypy/util.py", line 509, in __init__
  File "mypy/util.py", line 565, in initialize_unix_colors
TypeError: bytes object expected; got None

Reverting to 0.910 resolves the issue.

This may be a dependency or packaging issue - switching between 0.910 and 0.920 also causes toml to be updated from 0.10.2 to 2.0.0.

Python 3.9.7
Poetry 1.1.12

@mjog mjog added the bug mypy got something wrong label Dec 16, 2021
@KotlinIsland
Copy link
Contributor

This may be a dependency or packaging issue - switching between 0.910 and 0.920 also causes toml to be updated from 0.10.2 to 2.0.0.

There is no toml 2.0.0, mypy switched from toml to tomli.

@hauntsaninja
Copy link
Collaborator

So it seems like curses.tigetstr('bold') is returning None, when typeshed promises that it returns bytes. Because mypy is compiled with mypyc, there is an element of runtime type checking. Can you confirm whether that's the case on your system?

The weird thing is I'm not seeing any relevant changes between 0.910 and 0.920

@JelleZijlstra
Copy link
Member

Looking at the source code (https://github.com/python/cpython/blob/3.9/Modules/_cursesmodule.c#L4152), tigetstr returns None if tigetstr() from the underlying C library returned an error. And https://docs.oracle.com/cd/E36784_01/html/E36880/tigetstr-3curses.html says "With the tigetstr() routine, the value (char *)−1 is returned if capname is not a string capability." I don't know what would affect whether "bold" is a string capability, but we should at least change typeshed.

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 17, 2021

I suspect that this started causing issues since mypyc only recently started type checking bytes values at runtime -- previously mypyc treated bytes types as Any. I guess we should update the return values of some functions in curses to have optional types.

@JelleZijlstra
Copy link
Member

I'll submit a PR to typeshed.

JelleZijlstra added a commit to python/typeshed that referenced this issue Dec 17, 2021
Fixes python/mypy#11768.

I reviewed all the function definitions in https://github.com/python/cpython/blob/3.9/Modules/_cursesmodule.c#L4152 and checked their types. I did not review the constants or the methods on the Window object.
@wbolster
Copy link

wbolster commented Dec 17, 2021

seeing this as well. curses.tigetstr('bold') indeed does return None inside emacs. this breaks ‘compilation’, e.g. running mypy and linting scripts, which is very usually very convenient since it makes jumping to errors very ergonomic.

Python 3.10.1+ (heads/3.10:99c7232, Dec  8 2021, 17:39:30) [GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> python.el: native completion setup loaded
>>> import curses
>>> curses.tigetstr('bold')
>>> print(curses.tigetstr('bold'))
None

same for other calls in initialize_unix_colors():

>>> curses.tigetstr('bold')
>>> curses.tigetstr('smul')
>>> curses.tigetstr('setaf')
>>> curses.tigetstr('cup')

@wbolster
Copy link

wbolster commented Dec 17, 2021

this can be easily reproduced by setting TERM=dumb, which likely means this manifests in many other places, not just in emacs, e.g. in CI runners.

$ TERM=dumb mypy
Traceback (most recent call last):
  File ".../bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File ".../lib/python3.10/site-packages/mypy/__main__.py", line 11, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "mypy/main.py", line 70, in main
  File "mypy/util.py", line 509, in __init__
  File "mypy/util.py", line 565, in initialize_unix_colors
TypeError: bytes object expected; got None

(perhaps this issue title can be changed to reflect that this is TERM related, not emacs)

hauntsaninja pushed a commit to hauntsaninja/mypy that referenced this issue Dec 17, 2021
To help with python#11768

We'll also need to cherry pick python#11781
hauntsaninja pushed a commit to hauntsaninja/mypy that referenced this issue Dec 17, 2021
To help with python#11768

We'll also need to cherry pick python#11781
@hauntsaninja hauntsaninja changed the title Regression: TypeError in initialize_unix_colors in 0.920 when run under emacs Regression: crash in initialize_unix_colors in 0.920 Dec 17, 2021
@hauntsaninja hauntsaninja added crash and removed bug mypy got something wrong labels Dec 17, 2021
JukkaL pushed a commit that referenced this issue Dec 20, 2021
Solves #11768 in the 0.930 branch

Found by python/typeshed#6620

Co-authored-by: hauntsaninja <>
Co-authored-by: Jelle Zijlstra <[email protected]>
@wbolster
Copy link

i think this issue can be closed since 0.921 (just released) fixes it

@hauntsaninja
Copy link
Collaborator

Yup, should be fixed in 0.921 and 0.930.
Weirdly, it might not be fixed on master just yet, since I don't think we've updated typeshed (although most people don't use mypyc with master, so it's not a problem until 0.940). Updating typeshed will almost certainly just happen on its own, but I'll leave it open just in case.

@mjog
Copy link
Author

mjog commented Dec 25, 2021

Confirming fixed for me in 0.921. Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants