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

MyPy daemon version 1.7.0+ crashes if colorama is installed and reporting (cubertura) is used. #16678

Closed
sshishov opened this issue Dec 19, 2023 · 3 comments · Fixed by #16801

Comments

@sshishov
Copy link

Bug Report

MyPy daemon version 1.7.0+ crashes if colorama is installed and reporting (cubertura) is used.
The issue is related to this MR: #16252
The wrapper should implement "ALL" abstract method to make sure that it is not broken somewhere down the stream. IO has standard interface which can be used anywhere in the code (plugins or other 3rd party libraries).
More specifically the wrapper does not implement flush which is used in colorama

To Reproduce

Install latest:

  • mypy 1.7.0+
  • colorama 0.4.6+

Run dmypy run with cubertura reporting enabled in config file

In pyproject.toml or mypy.ini put the following lines:

cobertura_xml_report = "src/mypy-reports"
xslt_txt_report = "src/mypy-reports"
xslt_html_report = "src/mypy-reports"

Expected Behavior

MyPy daemon should not crash

Actual Behavior

MyPy daemon crash with exception:

python -m mypy.dmypy run .

dmypy: Ignoring report generation settings. Start/restart cannot generate reports.
Daemon started
Daemon crashed!
Traceback (most recent call last):
  File "mypy/dmypy_server.py", line 236, in serve
  File "mypy/dmypy_server.py", line 285, in run_command
  File "mypy/dmypy_server.py", line 353, in cmd_run
  File "mypy/dmypy_server.py", line 424, in check
  File "mypy/dmypy_server.py", line 463, in initialize_fine_grained
  File "mypy/build.py", line 189, in build
  File "mypy/build.py", line 286, in _build
  File "mypy/report.py", line 95, in finish
  File "mypy/report.py", line 698, in on_finish
  File "${VENV}/lib/python3.11/site-packages/colorama/ansitowin32.py", line 47, in write
    self.__convertor.write(text)
  File "${VENV}/lib/python3.11/site-packages/colorama/ansitowin32.py", line 177, in write
    self.write_and_convert(text)
  File "${VENV}/lib/python3.11/site-packages/colorama/ansitowin32.py", line 205, in write_and_convert
    self.write_plain_text(text, cursor, len(text))
  File "${VENV}/lib/python3.11/site-packages/colorama/ansitowin32.py", line 211, in write_plain_text
    self.wrapped.flush()
    ^^^^^^^^^^^^^^^^^^
AttributeError: 'WriteToConn' object has no attribute 'flush'

Generated Cobertura report:%

Your Environment

  • Mypy version used: 1.7.1
  • Mypy command-line flags: dmypy run -- --cache-fine-grained
  • Mypy configuration options from mypy.ini (and other config files):
cobertura_xml_report = "src/mypy-reports"
xslt_txt_report = "src/mypy-reports"
xslt_html_report = "src/mypy-reports"
plugins = [
    "mypy_django_plugin.main",
    "mypy_drf_plugin.main",
    "pydantic.mypy",
]
enable_error_code = [
    "ignore-without-code",
    "possibly-undefined",
    "redundant-self",
    "unused-awaitable",
]
strict = true
warn_unreachable = true
ignore_missing_imports = true
  • Python version used: 3.11.5
@sshishov sshishov added the bug mypy got something wrong label Dec 19, 2023
@AlexWaygood AlexWaygood added crash topic-daemon dmypy and removed bug mypy got something wrong labels Dec 19, 2023
@akshetpandey
Copy link

akshetpandey commented Jan 9, 2024

Also ran into this issue and I am not use cobertura, but do have colorama installed. Not sure if the django mypy plugins is causing it to be used but its the same crash.

Update: django mypy plugin initializes django with initializes colorama

@akshetpandey
Copy link

Work around:

def is_running_as_mypy() -> bool:
    binary_name = sys.argv[0]
    return binary_name.endswith(('/dmypy', '/mypy'))

# Add this to somewhere top of the import/startup chain
# mypy 1.7/1.8 daemon crashes when initialized with colorama wrapping the output stream
if is_running_as_mypy():
    colorama.deinit()
    colorama.init(wrap=False)

meshy added a commit to meshy/mypy that referenced this issue Jan 19, 2024
This class replaced stdout and stderr to capture output, but caused
issues because it didn't implement the full API of sys.stdout and
sys.stderr.

By fully stubbing the TextIO API we prevent issues with other code
which uses more of the API than we had previously accounted for.

Fixes python#16678
@meshy
Copy link
Contributor

meshy commented Jan 19, 2024

I've written a PR which I hope addresses this issue.

meshy added a commit to meshy/mypy that referenced this issue Jan 19, 2024
This class replaced stdout and stderr to capture output, but caused
issues because it didn't implement the full API of sys.stdout and
sys.stderr.

By fully stubbing the TextIO API we prevent issues with other code
which uses more of the API than we had previously accounted for.

Fixes python#16678
meshy added a commit to meshy/mypy that referenced this issue Jan 19, 2024
This class replaced stdout and stderr to capture output, but caused
issues because it didn't implement the full API of sys.stdout and
sys.stderr.

By fully stubbing the TextIO API we prevent issues with other code
which uses more of the API than we had previously accounted for.

Fixes python#16678
meshy added a commit to meshy/mypy that referenced this issue Jan 20, 2024
This class replaced stdout and stderr to capture output, but caused
issues because it didn't implement the full API of sys.stdout and
sys.stderr.

By fully stubbing the TextIO API we prevent issues with other code
which uses more of the API than we had previously accounted for.

Fixes python#16678
meshy added a commit to meshy/mypy that referenced this issue Jan 20, 2024
This class replaced stdout and stderr to capture output, but caused
issues because it didn't implement the full API of sys.stdout and
sys.stderr.

By fully stubbing the TextIO API we prevent issues with other code
which uses more of the API than we had previously accounted for.

Fixes python#16678
hauntsaninja pushed a commit that referenced this issue Jan 30, 2024
`WriteToConn` replaces stdout and stderr to capture output, but causes
issues because it doesn't implement the `TextIO` API (as expected of
`sys.stdout` and `sys.stderr`).

By stubbing the rest of the `TextIO` API we prevent issues with other
code which uses more of the API than we had previously accounted for.

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

Successfully merging a pull request may close this issue.

4 participants