Skip to content

Commit

Permalink
Revert "NamedTemporaryFile works different on Windows (#26)"
Browse files Browse the repository at this point in the history
This reverts commit 8363a8b.
  • Loading branch information
gvanrossum committed Nov 22, 2017
1 parent 62a622e commit 409fe62
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pyannotate_tools/annotations/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, target_stream=tf)
generate_annotations_json(infile, tf.name)

# Run pass 3 reading from a temporary file.
FixAnnotateJson.stub_json_file = tf.name
Expand Down
15 changes: 6 additions & 9 deletions pyannotate_tools/annotations/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import json

from typing import List, Optional, IO
from typing import List
from mypy_extensions import TypedDict

from pyannotate_tools.annotations.types import ARG_STAR, ARG_STARSTAR
Expand All @@ -22,16 +22,16 @@
'samples': int})


def generate_annotations_json(source_path, target_path, source_stream=None, target_stream=None):
# type: (str, str, Optional[IO[str]], Optional[IO[str]]) -> None
def generate_annotations_json(source_path, target_path):
# type: (str, str) -> None
"""Produce annotation data JSON file from a JSON file with runtime-collected types.
Data formats:
* The source JSON is a list of pyannotate_tools.annotations.parse.RawEntry items.
* The output JSON is a list of FunctionData items.
"""
items = parse_json(source_path, source_stream)
items = parse_json(source_path)
results = []
for item in items:
arg_types, return_type = infer_annotation(item.type_comments)
Expand All @@ -55,8 +55,5 @@ def generate_annotations_json(source_path, target_path, source_stream=None, targ
'samples': item.samples
} # type: FunctionData
results.append(data)
if target_stream:
json.dump(results, target_stream, sort_keys=True, indent=4)
else:
with open(target_path, 'w') as f:
json.dump(results, f, sort_keys=True, indent=4)
with open(target_path, 'w') as f:
json.dump(results, f, sort_keys=True, indent=4)
13 changes: 5 additions & 8 deletions pyannotate_tools/annotations/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import re
import sys

from typing import Any, List, Mapping, Set, Tuple, Optional, IO
from typing import Any, List, Mapping, Set, Tuple
from typing_extensions import Text
from mypy_extensions import NoReturn, TypedDict

Expand Down Expand Up @@ -86,17 +86,14 @@ def __init__(self, comment):
self.comment = comment


def parse_json(path, stream=None):
# type: (str, Optional[IO[str]]) -> List[FunctionInfo]
def parse_json(path):
# type: (str) -> List[FunctionInfo]
"""Deserialize a JSON file containing runtime collected types.
The input JSON is expected to to have a list of RawEntry items.
"""
if stream:
data = json.load(stream) # type: List[RawEntry]
else:
with open(path) as f:
data = json.load(f)
with open(path) as f:
data = json.load(f) # type: List[RawEntry]
result = []

def assert_type(value, typ):
Expand Down
21 changes: 9 additions & 12 deletions pyannotate_tools/annotations/tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import textwrap
import unittest

from typing import Iterator, Tuple, IO
from typing import Iterator

from pyannotate_tools.annotations.infer import InferError
from pyannotate_tools.annotations.main import generate_annotations_json
Expand All @@ -25,12 +25,10 @@ def test_generation(self):
}
]
"""
target = tempfile.NamedTemporaryFile(mode='w+')
with self.temporary_json_file(data) as source:
generate_annotations_json(source.name, target.name, source_stream=source, target_stream=target)
target = tempfile.NamedTemporaryFile(mode='r')
with self.temporary_json_file(data) as source_path:
generate_annotations_json(source_path, target.name)

target.flush()
target.seek(0)
actual = target.read()
actual = actual.replace(' \n', '\n')
expected = textwrap.dedent("""\
Expand Down Expand Up @@ -68,18 +66,17 @@ def test_ambiguous_kind(self):
]
"""
with self.assertRaises(InferError) as e:
with self.temporary_json_file(data) as source:
generate_annotations_json(source.name, '/dummy', source_stream=source)
with self.temporary_json_file(data) as source_path:
generate_annotations_json(source_path, '/dummy')
assert str(e.exception) == textwrap.dedent("""\
Ambiguous argument kinds:
(List[int], str) -> None
(List[int], *str) -> None""")

@contextlib.contextmanager
def temporary_json_file(self, data):
# type: (str) -> Iterator[IO[str]]
with tempfile.NamedTemporaryFile(mode='w+') as source:
# type: (str) -> Iterator[str]
with tempfile.NamedTemporaryFile(mode='w') as source:
source.write(data)
source.flush()
source.seek(0)
yield source
yield source.name
5 changes: 2 additions & 3 deletions pyannotate_tools/annotations/tests/parse_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ def test_parse_json(self):
}
]
"""
with tempfile.NamedTemporaryFile(mode='w+') as f:
with tempfile.NamedTemporaryFile(mode='w') as f:
f.write(data)
f.flush()
f.seek(0)

result = parse_json(f.name, f)
result = parse_json(f.name)
assert len(result) == 1
item = result[0]
assert item.path == 'pkg/thing.py'
Expand Down

0 comments on commit 409fe62

Please sign in to comment.