Skip to content

Commit cf09e8b

Browse files
committed
Convert from type comments to annotations
I've been slowly phasing out the type comments as I modify various parts of the codebase. These were the files affected from the attrs removal.
1 parent 0b09c45 commit cf09e8b

File tree

4 files changed

+429
-332
lines changed

4 files changed

+429
-332
lines changed

chalice/cli/newproj.py

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@
4343

4444

4545
TEMPLATES_DIR = os.path.join(
46-
os.path.dirname(
47-
os.path.dirname(os.path.abspath(__file__))),
48-
'templates'
46+
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'templates'
4947
)
5048
VAR_REF_REGEX = r'{{(.*?)}}'
5149
IGNORE_FILES = ['metadata.json', '*.pyc']
@@ -55,8 +53,9 @@ class BadTemplateError(Exception):
5553
pass
5654

5755

58-
def create_new_project_skeleton(project_name, project_type='legacy'):
59-
# type: (str, Optional[str]) -> None
56+
def create_new_project_skeleton(
57+
project_name: str, project_type: Optional[str] = 'legacy'
58+
) -> None:
6059
osutils = OSUtils()
6160
all_projects = list_available_projects(TEMPLATES_DIR, osutils)
6261
project = [p for p in all_projects if p.key == project_type][0]
@@ -86,28 +85,36 @@ def description(self) -> str:
8685

8786

8887
class ProjectCreator(object):
89-
def __init__(self, osutils=None):
90-
# type: (Optional[OSUtils]) -> None
88+
def __init__(self, osutils: Optional[OSUtils] = None) -> None:
9189
if osutils is None:
9290
osutils = OSUtils()
9391
self._osutils = osutils
9492

95-
def create_new_project(self, source_dir, destination_dir, template_kwargs):
96-
# type: (str, str, Dict[str, Any]) -> None
97-
for full_src_path, full_dst_path in self._iter_files(source_dir,
98-
destination_dir):
93+
def create_new_project(
94+
self,
95+
source_dir: str,
96+
destination_dir: str,
97+
template_kwargs: Dict[str, Any],
98+
) -> None:
99+
for full_src_path, full_dst_path in self._iter_files(
100+
source_dir, destination_dir
101+
):
99102
dest_dir = self._osutils.dirname(full_dst_path)
100103
if not self._osutils.directory_exists(dest_dir):
101104
self._osutils.makedirs(dest_dir)
102-
contents = self._osutils.get_file_contents(full_src_path,
103-
binary=False)
105+
contents = self._osutils.get_file_contents(
106+
full_src_path, binary=False
107+
)
104108
templated_contents = get_templated_content(
105-
contents, template_kwargs)
109+
contents, template_kwargs
110+
)
106111
self._osutils.set_file_contents(
107-
full_dst_path, templated_contents, binary=False)
112+
full_dst_path, templated_contents, binary=False
113+
)
108114

109-
def _iter_files(self, source_dir, destination_dir):
110-
# type: (str, str) -> Iterator[Tuple[str, str]]
115+
def _iter_files(
116+
self, source_dir: str, destination_dir: str
117+
) -> Iterator[Tuple[str, str]]:
111118
for rootdir, _, filenames in self._osutils.walk(source_dir):
112119
for filename in filenames:
113120
if self._should_ignore(filename):
@@ -116,37 +123,38 @@ def _iter_files(self, source_dir, destination_dir):
116123
# The starting index needs `+ 1` to account for the
117124
# trailing `/` char (e.g. foo/bar -> foo/bar/).
118125
full_dst_path = os.path.join(
119-
destination_dir, full_src_path[len(source_dir) + 1:])
126+
destination_dir, full_src_path[len(source_dir) + 1:]
127+
)
120128
yield full_src_path, full_dst_path
121129

122-
def _should_ignore(self, filename):
123-
# type: (str) -> bool
130+
def _should_ignore(self, filename: str) -> bool:
124131
for ignore in IGNORE_FILES:
125132
if fnmatch.fnmatch(filename, ignore):
126133
return True
127134
return False
128135

129136

130-
def get_templated_content(contents, template_kwargs):
131-
# type: (str, Dict[str, Any]) -> str
132-
def lookup_var(match):
133-
# type: (Match) -> str
137+
def get_templated_content(
138+
contents: str, template_kwargs: Dict[str, Any]
139+
) -> str:
140+
def lookup_var(match: Match) -> str:
134141
var_name = match.group(1)
135142
try:
136143
return template_kwargs[var_name]
137144
except KeyError:
138145
raise BadTemplateError(
139146
"Bad template, referenced template var that does not "
140-
"exist: '%s', for template contents:\n%s" % (var_name,
141-
contents)
147+
"exist: '%s', for template contents:\n%s"
148+
% (var_name, contents)
142149
)
143150

144151
new_contents = re.sub(VAR_REF_REGEX, lookup_var, contents)
145152
return new_contents
146153

147154

148-
def list_available_projects(templates_dir, osutils):
149-
# type: (str, OSUtils) -> List[ProjectTemplate]
155+
def list_available_projects(
156+
templates_dir: str, osutils: OSUtils
157+
) -> List[ProjectTemplate]:
150158
projects = []
151159
for dirname in sorted(osutils.get_directory_contents(templates_dir)):
152160
filename = osutils.joinpath(templates_dir, dirname, 'metadata.json')
@@ -156,14 +164,16 @@ def list_available_projects(templates_dir, osutils):
156164
return projects
157165

158166

159-
def getting_started_prompt():
160-
# type: () -> Dict[str, Any]
167+
def getting_started_prompt() -> Dict[str, Any]:
161168
print(WELCOME_PROMPT)
162169
projects = list_available_projects(TEMPLATES_DIR, OSUtils())
163170
questions = [
164171
inquirer.Text('project_name', message='Enter the project name'),
165-
inquirer.List('project_type', message='Select your project type',
166-
choices=[(p.description, p.key) for p in projects])
172+
inquirer.List(
173+
'project_type',
174+
message='Select your project type',
175+
choices=[(p.description, p.key) for p in projects],
176+
),
167177
]
168178
answers = inquirer.prompt(questions)
169179
return answers

0 commit comments

Comments
 (0)