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

NamedTemporaryFile works different on Windows #26

Merged
merged 3 commits into from Nov 22, 2017

Conversation

NeonGraal
Copy link
Contributor

Add optional stream parameters to generate_annotation_json and parse_json.

Hopefully fixes #25 but unable to run all tests due to lib2to3 quirks on Windows and Ubuntu

Add optional stream parameters to generate_annotation_json and parse_json
Copy link
Contributor

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

Please run mypy --ignore-missing-imports --strict-optional (as well as pytest) before pushing your next commit.

I also wonder if perhaps it wouldn't make more sense to pass the dicts around rather than going through a temporary file. (I only did it that way because Jukka had written his half of the code to write to a file exclusively.)

.gitignore Outdated
@@ -2,3 +2,5 @@
/*.egg-info
/build
/dist
.idea/
*.pyc
Copy link
Contributor

Choose a reason for hiding this comment

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

These two (but especially .idea) belong in your own global .gitignore (which you have to configure using something like

[core]
	excludesfile = /Users/guido/.gitignore_global

in your ~/.gitconfig.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll take them out.

I've gotten in the habit at work of not presuming that people's global .gitignores are correct.

@@ -42,7 +42,7 @@ def main():

# Run pass 2 with output written to a temporary file.
infile = args.type_info
generate_annotations_json(infile, tf.name)
generate_annotations_json(infile, tf.name, target_stream=tf.file)
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to use the .file attribute -- just pass tf, it's a file alright.

@@ -1,8 +1,9 @@
"""Main entry point to mypy annotation inference utility."""

import json
from io import IOBase
Copy link
Contributor

Choose a reason for hiding this comment

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

I wouldn't do that. I'd import IO from typing and use IO[str] as the type of the streams everywhere.

def generate_annotations_json(source_path, target_path):
# type: (str, str) -> None
def generate_annotations_json(source_path, target_path, source_stream=None, target_stream=None):
# type: (str, str, Optional[IOBase], Optional[IOBase]) -> None
Copy link
Contributor

Choose a reason for hiding this comment

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

IOBase -> IO[str]

data = json.load(stream)
else:
with open(path) as f:
data = json.load(f) # type: List[RawEntry]
Copy link
Contributor

Choose a reason for hiding this comment

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

The annotation should go on the first assignment to data.

generate_annotations_json(source_path, target.name)
target = tempfile.NamedTemporaryFile(mode='w+')
with self.temporary_json_file(data) as source:
generate_annotations_json(source[0], target.name, source_stream=source[1], target_stream=target.file)
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for .file here or anywhere.

@@ -76,7 +78,8 @@ def test_ambiguous_kind(self):
@contextlib.contextmanager
def temporary_json_file(self, data):
# type: (str) -> Iterator[str]
Copy link
Contributor

Choose a reason for hiding this comment

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

Update the type here too (Iterator[Tuple[str, IO[str]]]).

@NeonGraal
Copy link
Contributor Author

I've signed the CLA now too

Copy link
Contributor

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

Yay! Will merge soon.

@gvanrossum gvanrossum merged commit 8363a8b into dropbox:master Nov 22, 2017
@gvanrossum
Copy link
Contributor

Thanks! And fingers crossed for Windows. (I haven't managed to set up AppVeyor yet. Have you managed to test on Windows manually yet?)

@rowillia
Copy link
Contributor

This PR appears to have broken Python 3 -

$ pyannotate -w --type-info ./annotations.json $file
Traceback (most recent call last):
  File "/Users/rwilliams/src/oss/pyannotate/venv/bin/pyannotate", line 11, in <module>
    load_entry_point('pyannotate==1.0.2', 'console_scripts', 'pyannotate')()
  File "/Users/rwilliams/src/oss/pyannotate/venv/lib/python3.6/site-packages/pyannotate-1.0.2-py3.6.egg/pyannotate_tools/annotations/__main__.py", line 45, in main
  File "/Users/rwilliams/src/oss/pyannotate/venv/lib/python3.6/site-packages/pyannotate-1.0.2-py3.6.egg/pyannotate_tools/annotations/main.py", line 59, in generate_annotations_json
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 180, in dump
    fp.write(chunk)
  File "/Users/rwilliams/src/oss/pyannotate/venv/lib/python3.6/tempfile.py", line 483, in func_wrapper
    return func(*args, **kwargs)
TypeError: a bytes-like object is required, not 'str'

We should probably write a test that exercises main, perhaps just run through the example?

@gvanrossum
Copy link
Contributor

Ouch. Yeah, we need a test for that! I'm submitting a revert as a PR (#42), but if you make a PR that fixes it (with a test) we can apply that instead.

@gvanrossum
Copy link
Contributor

gvanrossum commented Nov 22, 2017 via email

gvanrossum added a commit that referenced this pull request Nov 22, 2017
Reverts #26

It broke main() on Python 3. I will redo this without silly temp files.
gvanrossum pushed a commit that referenced this pull request Nov 22, 2017
Fixes #26.

There are some remaining TODO items:
- Add annotations to fixers
- Add a test for main and __main__
gvanrossum added a commit that referenced this pull request Nov 23, 2017
Fixes #26.

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

Successfully merging this pull request may close these issues.

Permission denied for Temp file on Windows
3 participants