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 complains if file replaced by shadow-file doesn't exist #4746

Open
cpitclaudel opened this issue Mar 16, 2018 · 7 comments
Open

MyPy complains if file replaced by shadow-file doesn't exist #4746

cpitclaudel opened this issue Mar 16, 2018 · 7 comments

Comments

@cpitclaudel
Copy link

This is a follow-up to #2119. I think this a bug report, but maybe it's a feature request, or even possibly a documentation request.

I'm surprised by the following behavior of --shadow-file SOURCE_FILE SHADOW_FILE:

$ cd /tmp/
$ mkdir python
$ cd python/
$ echo "x = 1 + 'A'" > shadow.py
$ mypy --shadow-file error.py shadow.py error.py #1
mypy: can't read file 'error.py': No such file or directory
$ mypy --shadow-file error.py shadow.py shadow.py #2
mypy: can't read file 'shadow.py': No such file or directory
$ mypy shadow.py #3
shadow.py:1: error: Unsupported operand types for + ("int" and "str")

I see two problems here:

  • I expected # (1) to behave just as if there had been a file error.py with the contents of shadow.py, returning the same as # (3). This use case happens concretely when a user of Emacs creates a new file (but hasn't saved it to disk yet). In that case we create a temporary file with the contents of the unsaved file, and pass that as shadow.py, with error.py being the name of the yet-unsaved file.

  • In # (2) I did not expected the --shadow-file option to make a difference, since I remapped error.py to shadow.py and then asked about shadow.py (which shouldn't be affected by the unrelated mapping). Additionally, the error seems wrong (the file shadow.py exists).

I'm using mypy 0.570 and Python 3.5.2 (I have not tried with mypy from git master)

Am I misunderstanding the way --shadow-file is used?

Thanks!

@gvanrossum
Copy link
Member

Your invocation (1) is correct, but the error.py file should still exist (its existence is checked before the shadowing goes into effect, though its contents is irrelevant).

In (2) the shadow flag has no effect because you're not type-checking error.py.

You'll find that if you create an empty error.py, (1) will work correctly -- it will report the same error as (3), except the filename will be error.py.

@cpitclaudel
Copy link
Author

Thanks!

In (2) the shadow flag has no effect because you're not type-checking error.py.

But it does have an effect: mypy produces a nonsensical error message. Is that intended?

You'll find that if you create an empty error.py, (1) will work correctly -- it will report the same error as (3), except the filename will be error.py.

I'm well aware of this :) But it's inconvenient. Is there a strong reason to require that the file exist?

@gvanrossum
Copy link
Member

Oh, I wonder if you've got the arguments to --shadow-file reversed. See #4712

@gvanrossum gvanrossum reopened this Mar 16, 2018
@cpitclaudel
Copy link
Author

cpitclaudel commented Mar 16, 2018

Sorry if I'm being slow. I'm struggling to reconcile the docs (and the behavior that I observe) with your comment. In your comment you say that the correct invocation is mypy --shadow-file flycheck_tt.py tt.py tt.py (flycheck_tt is the temporary one; tt is the real one). But the docs says this:

  --shadow-file SOURCE_FILE SHADOW_FILE
                            Typecheck SHADOW_FILE in place of SOURCE_FILE.

and indeed when I try thing in the order suggested by the docstring, everything works as expected:

$ echo '1 + "A"' > temp.py
$ echo '"no error"' > real.py
$ mypy --shadow-file real.py temp.py real.py 
real.py:1: error: Unsupported operand types for + ("int" and "str")

whereas in the order that you suggest, I don't get errors:

$ mypy --shadow-file temp.py real.py real.py 

My core issue is different, though; I'd like one of these (I'm not sure which one anymore :/) to work:

$ mypy --shadow-file temp.py nonexistent.py nonexistent.py
mypy: can't read file 'nonexistent.py': No such file or directory
$ mypy --shadow-file nonexistent.py temp.py nonexistent.py
mypy: can't read file 'nonexistent.py': No such file or directory 

@cpitclaudel
Copy link
Author

… and I made the mistake of mixing two bugs in one in my original report — sorry about this. The following seems suspicious to me:

$ touch a.py
$ mypy --shadow-file a.py nonexistent.py a.py
mypy: can't read file 'a.py': No such file or directory
$ mypy --shadow-file nonexistent.py a.py a.py
mypy: can't read file 'a.py': No such file or directory

(I could understand mypy complaining about nonexistent.py, but it seems odd to complain about a.py, which does exists)

@gvanrossum
Copy link
Member

gvanrossum commented Mar 16, 2018 via email

@gvanrossum
Copy link
Member

Also, if you want to help, you can put in some print statements (or better, use pdb) and try to find the bug(s) in the code.

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

No branches or pull requests

2 participants