Skip to content

Replace assert with warning #7567

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

Closed
wants to merge 1 commit into from
Closed

Replace assert with warning #7567

wants to merge 1 commit into from

Conversation

chriselion
Copy link

Addresses issue #7510 (along with a few others that have a different root cause. The conclusion from the discussion was that if we check the same file twice, something is probably weird, but mypy shouldn't crash.

I still need to add a test for this, but I didn't see a test file corresponding to this one. Suggestions on where to add it?

@@ -310,7 +310,11 @@ def report(self,
self.add_error_info(info)

def _add_error_info(self, file: str, info: ErrorInfo) -> None:
assert file not in self.flushed_files
if file in self.flushed_files:
self.report(info.line, None,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied the basic format for the warning from

self._errors.report(line, None, msg, severity='warning', file=path)

Note that only_once=True is necessary to prevent recursing infinitely.

self.report(info.line, None,
"Trying to report an error in a file that has already been flushed."
" Skipping...", severity='warning', file=file, only_once=True)
return
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I think I want to return here so that the errors don't get logged multiple times. But that means this warning gets skipped too :/

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for PR!

One general comment: I think we should give this warning, even if there were no errors in the file, so maybe this is not the best place to show it. I would even make this a blocker error somewhere in mypy/build.py, similarly to how we ban duplicate modules.

Also maybe try adding some diagnostics to the error message, something like:

some/bad.py: error: This file name can be interpreted as more than one module
some/bad.py: error: When read from MYPYPATH: src.some.bad
some/bad.py: error: When read from command line: some.bad

The latter is optional.

@chriselion
Copy link
Author

I'm hesitant to make this a blocker error, since (as an end user) it's not clear to me how to resolve it. It would also be a potentially breaking change for anybody that's currently in this situation but not hitting any errors and thus not hitting the assert (although that only affects people with perfect code 😄).

Also, I should clarify that I'm encountering this without setting MYPYPATH; it happens with --namespace-packages

@ilevkivskyi
Copy link
Member

I'm hesitant to make this a blocker error, since (as an end user) it's not clear to me how to resolve it.

Even if you will not make it a blocker, it effectively will be one, since there is no line number it can be attributed to, one can't put a # type: ignore to silence it. I would say we should try this and see how much troubles it will cause.

Also conceptually if there is this error, it means that something went really wrong, and likely type-checking results can't be trusted.

@msullivan
Copy link
Collaborator

Agreed that it should be a blocker

Copy link
Collaborator

@msullivan msullivan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error should be a blocker

@chriselion
Copy link
Author

I worked around the original problem by not using namespace packages.

@chriselion chriselion closed this Feb 21, 2020
@chriselion chriselion deleted the fix-flushed_files-assert branch February 21, 2020 23:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants