Repository: python/mypy

Issue:
pyproject.toml requires mypy 1.4.0-1.5.0 + mypycify in setup.py: failure (AssertionError)
**Bug Report**

When upgrading a `mypycify`-using Python project from mypy 1.3.0 to a newer version, a `pip install` of my project fails where it does not with mypy 1.3.0

**To Reproduce**

``` bash
git clone https://github.com/common-workflow-language/cwltool
cd cwltool
sed  -i 's/mypy==1.3.0/mypy==1.4.0/' pyproject.toml  # or to 1.4.1, or 1.5.0
MYPYPATH=mypy-stubs CWLTOOL_USE_MYPYC=1 pip install  .
```

**Expected Behavior**

```
$ MYPYPATH=mypy-stubs CWLTOOL_USE_MYPYC=1 pip install .
Processing /home/michael/cwltool
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Installing backend dependencies ... done
  Preparing metadata (pyproject.toml) ... done
Requirement already satisfied: setuptools in ./env3.11/lib/python3.11/site-packages (from cwltool==3.1) (66.1.1)

[snip]

Building wheels for collected packages: cwltool
  Building wheel for cwltool (pyproject.toml) ... done
  Created wheel for cwltool: filename=cwltool-3.1-cp311-cp311-linux_x86_64.whl size=3672349 sha256=c467454417b08ede66d0832b6a17e7b00cded2d7e6073be12100d83746bb04ae
  Stored in directory: /tmp/pip-ephem-wheel-cache-z0z1cpyr/wheels/ee/cd/42/595fc105979e9f4d5af86398d7a0f9de9008e61383a87ccdb1
Successfully built cwltool
```
**Actual Behavior**

(passing `--no-binary mypy` to `pip install` to get a more interesting stacktrace, the same error occurs without it)

```
$ MYPYPATH=mypy-stubs CWLTOOL_USE_MYPYC=1 pip install  . --no-binary mypy
Processing /home/michael/cwltool
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error
  
  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [35 lines of output]
      Traceback (most recent call last):
        File "/home/michael/cwltool/env3.11/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
          main()
        File "/home/michael/cwltool/env3.11/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/michael/cwltool/env3.11/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
          return hook(config_settings)
                 ^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-rnls4s4_/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 341, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=['wheel'])
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-rnls4s4_/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 323, in _get_build_requires
          self.run_setup()
        File "/tmp/pip-build-env-rnls4s4_/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 338, in run_setup
          exec(code, locals())
        File "<string>", line 91, in <module>
        File "/tmp/pip-build-env-rnls4s4_/overlay/lib/python3.11/site-packages/mypyc/build.py", line 511, in mypycify
          groups, group_cfilenames = mypyc_build(
                                     ^^^^^^^^^^^^
        File "/tmp/pip-build-env-rnls4s4_/overlay/lib/python3.11/site-packages/mypyc/build.py", line 421, in mypyc_build
          group_cfiles, ops_text = generate_c(
                                   ^^^^^^^^^^^
        File "/tmp/pip-build-env-rnls4s4_/overlay/lib/python3.11/site-packages/mypyc/build.py", line 233, in generate_c
          emit_messages(options, errors.new_messages(), t2 - t1)
                                 ^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-rnls4s4_/overlay/lib/python3.11/site-packages/mypyc/errors.py", line 25, in new_messages
          return self._errors.new_messages()
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-rnls4s4_/overlay/lib/python3.11/site-packages/mypy/errors.py", line 871, in new_messages
          msgs.extend(self.file_messages(path))
                      ^^^^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-rnls4s4_/overlay/lib/python3.11/site-packages/mypy/errors.py", line 857, in file_messages
          assert self.read_source
      AssertionError
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

```

Same for mypy 1.4.1 and 1.5.0

**Your Environment**

- Mypy version used: 1.4.0+
- Python version used: 3.11.2



Discussion:
I've git bisected this error down to 768ba66af4c90c14c19275896067a4e4fe6f2f46 by @koogoro from #15070
(using its parent commit 3d9661c91d5dfaf3ae0d3ca5624867cdf449da77 I don't get an error).

Therefore the `topic-mypyc` label should probably be removed
Ping

Resolving patch:
diff --git a/mypy/errors.py b/mypy/errors.py
--- a/mypy/errors.py
+++ b/mypy/errors.py
@@ -909,8 +909,7 @@ def file_messages(self, path: str) -> list[str]:
             return []
         self.flushed_files.add(path)
         source_lines = None
-        if self.options.pretty:
-            assert self.read_source
+        if self.options.pretty and self.read_source:
             source_lines = self.read_source(path)
         return self.format_messages(self.error_info_map[path], source_lines)
 


Test patch: