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

Migrate to mypy py3 mode #5760

Merged
merged 3 commits into from
Dec 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ paths =
.

[mypy]
python_version = 2.7
show_column_numbers = True
show_error_context = True
ignore_missing_imports = True
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
'flake8>=3.5.0',
'flake8-import-order',
'mypy>=0.470',
'docutils-stubs',
],
}

Expand Down
2 changes: 1 addition & 1 deletion sphinx/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@

from sphinx.cmd.build import main

sys.exit(main(sys.argv[1:])) # type: ignore
sys.exit(main(sys.argv[1:]))
4 changes: 2 additions & 2 deletions sphinx/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,10 @@ def _init_i18n(self):
user_locale_dirs, self.config.language, domains=['sphinx'],
charset=self.config.source_encoding):
catinfo.write_mo(self.config.language)
locale_dirs = [None, path.join(package_dir, 'locale')] + user_locale_dirs # type: ignore # NOQA
locale_dirs = [None, path.join(package_dir, 'locale')] + user_locale_dirs
else:
locale_dirs = []
self.translator, has_translation = locale.init(locale_dirs, self.config.language) # type: ignore # NOQA
self.translator, has_translation = locale.init(locale_dirs, self.config.language)
if self.config.language is not None:
if has_translation or self.config.language == 'en':
# "en" never needs to be translated
Expand Down
2 changes: 1 addition & 1 deletion sphinx/builders/applehelp.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def build_helpbook(self):

logger.info(bold(__('writing Info.plist... ')), nonl=True)
with open(path.join(contents_dir, 'Info.plist'), 'wb') as fb:
plistlib.dump(info_plist, fb) # type: ignore
plistlib.dump(info_plist, fb)
logger.info(__('done'))

# Copy the icon, if one is supplied
Expand Down
10 changes: 4 additions & 6 deletions sphinx/builders/changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,9 @@ def write(self, *ignored):
'show_copyright': self.config.html_show_copyright,
'show_sphinx': self.config.html_show_sphinx,
}
with open(path.join(self.outdir, 'index.html'), 'w', # type: ignore
encoding='utf8') as f:
with open(path.join(self.outdir, 'index.html'), 'w', encoding='utf8') as f:
f.write(self.templates.render('changes/frameset.html', ctx))
with open(path.join(self.outdir, 'changes.html'), 'w', # type: ignore
encoding='utf8') as f:
with open(path.join(self.outdir, 'changes.html'), 'w', encoding='utf8') as f:
f.write(self.templates.render('changes/versionchanges.html', ctx))

hltext = ['.. versionadded:: %s' % version,
Expand All @@ -135,7 +133,7 @@ def hl(no, line):

logger.info(bold(__('copying source files...')))
for docname in self.env.all_docs:
with open(self.env.doc2path(docname), # type: ignore
with open(self.env.doc2path(docname),
encoding=self.env.config.source_encoding) as f:
try:
lines = f.readlines()
Expand All @@ -144,7 +142,7 @@ def hl(no, line):
continue
targetfn = path.join(self.outdir, 'rst', os_path(docname)) + '.html'
ensuredir(path.dirname(targetfn))
with open(targetfn, 'w', encoding='utf-8') as f: # type: ignore
with open(targetfn, 'w', encoding='utf-8') as f:
text = ''.join(hl(i + 1, line) for (i, line) in enumerate(lines))
ctx = {
'filename': self.env.doc2path(docname, None),
Expand Down
2 changes: 1 addition & 1 deletion sphinx/builders/devhelp.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def write_index(title, refs, subitems):

# Dump the XML file
xmlfile = path.join(outdir, outname + '.devhelp.gz')
with gzip.open(xmlfile, 'w') as f: # type: ignore
with gzip.open(xmlfile, 'w') as f:
tree.write(f, 'utf-8')


Expand Down
21 changes: 10 additions & 11 deletions sphinx/builders/gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def add(self, msg, origin):
if msg not in self.metadata: # faster lookup in hash
self.messages.append(msg)
self.metadata[msg] = []
self.metadata[msg].append((origin.source, origin.line, origin.uid))
self.metadata[msg].append((origin.source, origin.line, origin.uid)) # type: ignore


class MsgOrigin:
Expand Down Expand Up @@ -198,7 +198,7 @@ def should_write(filepath, new_content):
if not path.exists(filepath):
return True
try:
with open(filepath, encoding='utf-8') as oldpot: # type: ignore
with open(filepath, encoding='utf-8') as oldpot:
old_content = oldpot.read()
old_header_index = old_content.index('"POT-Creation-Date:')
new_header_index = new_content.index('"POT-Creation-Date:')
Expand Down Expand Up @@ -246,10 +246,10 @@ def _extract_from_template(self):

extract_translations = self.templates.environment.extract_translations

for template in status_iterator(files, __('reading templates... '), "purple", # type: ignore # NOQA
for template in status_iterator(files, __('reading templates... '), "purple",
len(files), self.app.verbosity):
try:
with open(template, encoding='utf-8') as f: # type: ignore
with open(template, encoding='utf-8') as f:
context = f.read()
for line, meth, msg in extract_translations(context):
origin = MsgOrigin(template, line)
Expand All @@ -272,7 +272,7 @@ def finish(self):
'ctime': datetime.fromtimestamp(
timestamp, ltz).strftime('%Y-%m-%d %H:%M%z'),
}
for textdomain, catalog in status_iterator(self.catalogs.items(), # type: ignore
for textdomain, catalog in status_iterator(self.catalogs.items(),
__("writing message catalogs... "),
"darkgreen", len(self.catalogs),
self.app.verbosity,
Expand All @@ -282,31 +282,30 @@ def finish(self):

pofn = path.join(self.outdir, textdomain + '.pot')
output = StringIO()
output.write(POHEADER % data) # type: ignore
output.write(POHEADER % data)

for message in catalog.messages:
positions = catalog.metadata[message]

if self.config.gettext_location:
# generate "#: file1:line1\n#: file2:line2 ..."
output.write("#: %s\n" % "\n#: ".join( # type: ignore
output.write("#: %s\n" % "\n#: ".join(
"%s:%s" % (canon_path(relpath(source, self.outdir)), line)
for source, line, _ in positions))
if self.config.gettext_uuid:
# generate "# uuid1\n# uuid2\n ..."
output.write("# %s\n" % "\n# ".join( # type: ignore
uid for _, _, uid in positions))
output.write("# %s\n" % "\n# ".join(uid for _, _, uid in positions))

# message contains *one* line of text ready for translation
message = message.replace('\\', r'\\'). \
replace('"', r'\"'). \
replace('\n', '\\n"\n"')
output.write('msgid "%s"\nmsgstr ""\n\n' % message) # type: ignore
output.write('msgid "%s"\nmsgstr ""\n\n' % message)

content = output.getvalue()

if should_write(pofn, content):
with open(pofn, 'w', encoding='utf-8') as pofile: # type: ignore
with open(pofn, 'w', encoding='utf-8') as pofile:
pofile.write(content)


Expand Down
12 changes: 6 additions & 6 deletions sphinx/builders/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ def copy_static_files(self):
ensuredir(path.join(self.outdir, '_static'))
# first, create pygments style file
with open(path.join(self.outdir, '_static', 'pygments.css'), 'w') as f:
f.write(self.highlighter.get_stylesheet()) # type: ignore
f.write(self.highlighter.get_stylesheet())
# then, copy translations JavaScript file
if self.config.language is not None:
jsfile = self._get_translations_js()
Expand Down Expand Up @@ -956,7 +956,7 @@ def load_indexer(self, docnames):
try:
searchindexfn = path.join(self.outdir, self.searchindex_filename)
if self.indexer_dumps_unicode:
with open(searchindexfn, encoding='utf-8') as ft: # type: ignore
with open(searchindexfn, encoding='utf-8') as ft:
self.indexer.load(ft, self.indexer_format)
else:
with open(searchindexfn, 'rb') as fb:
Expand Down Expand Up @@ -1137,8 +1137,8 @@ def warn(*args, **kwargs):
# outfilename's path is in general different from self.outdir
ensuredir(path.dirname(outfilename))
try:
with open(outfilename, 'w', # type: ignore
encoding=ctx['encoding'], errors='xmlcharrefreplace') as f:
with open(outfilename, 'w', encoding=ctx['encoding'],
errors='xmlcharrefreplace') as f:
f.write(output)
except (IOError, OSError) as err:
logger.warning(__("error writing file %s: %s"), outfilename, err)
Expand Down Expand Up @@ -1175,7 +1175,7 @@ def dump_search_index(self):
# first write to a temporary file, so that if dumping fails,
# the existing index won't be overwritten
if self.indexer_dumps_unicode:
with open(searchindexfn + '.tmp', 'w', encoding='utf-8') as ft: # type: ignore
with open(searchindexfn + '.tmp', 'w', encoding='utf-8') as ft:
self.indexer.dump(ft, self.indexer_format)
else:
with open(searchindexfn + '.tmp', 'wb') as fb:
Expand Down Expand Up @@ -1433,7 +1433,7 @@ def get_target_uri(self, docname, typ=None):
def dump_context(self, context, filename):
# type: (Dict, unicode) -> None
if self.implementation_dumps_unicode:
with open(filename, 'w', encoding='utf-8') as ft: # type: ignore
with open(filename, 'w', encoding='utf-8') as ft:
self.implementation.dump(context, ft, *self.additional_dump_args)
else:
with open(filename, 'wb') as fb:
Expand Down
4 changes: 2 additions & 2 deletions sphinx/builders/htmlhelp.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ def init(self):
def open_file(self, outdir, basename, mode='w'):
# type: (unicode, unicode, unicode) -> IO
# open a file with the correct encoding for the selected language
return open(path.join(outdir, basename), mode, # type: ignore
encoding=self.encoding, errors='xmlcharrefreplace')
return open(path.join(outdir, basename), mode, encoding=self.encoding,
errors='xmlcharrefreplace')

def update_page_context(self, pagename, templatename, ctx, event_arg):
# type: (unicode, unicode, Dict, unicode) -> None
Expand Down
2 changes: 1 addition & 1 deletion sphinx/builders/latex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def write_stylesheet(self):
f.write('\\NeedsTeXFormat{LaTeX2e}[1995/12/01]\n')
f.write('\\ProvidesPackage{sphinxhighlight}'
'[2016/05/29 stylesheet for highlighting with pygments]\n\n')
f.write(highlighter.get_stylesheet()) # type: ignore
f.write(highlighter.get_stylesheet())

def write(self, *ignored):
# type: (Any) -> None
Expand Down
3 changes: 1 addition & 2 deletions sphinx/builders/linkcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,7 @@ def write_doc(self, docname, doctree):

def write_entry(self, what, docname, line, uri):
# type: (unicode, unicode, int, unicode) -> None
with open(path.join(self.outdir, 'output.txt'), 'a', # type: ignore
encoding='utf-8') as output:
with open(path.join(self.outdir, 'output.txt'), 'a', encoding='utf-8') as output:
output.write("%s:%s: [%s] %s\n" % (self.env.doc2path(docname, None),
line, what, uri))

Expand Down
6 changes: 2 additions & 4 deletions sphinx/builders/qthelp.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ def build_qhp(self, outdir, outname):
nspace = nspace.lower()

# write the project file
with open(path.join(outdir, outname + '.qhp'), 'w', # type: ignore
encoding='utf-8') as f:
with open(path.join(outdir, outname + '.qhp'), 'w', encoding='utf-8') as f:
body = render_file('project.qhp', outname=outname,
title=self.config.html_title, version=self.config.version,
project=self.config.project, namespace=nspace,
Expand All @@ -150,8 +149,7 @@ def build_qhp(self, outdir, outname):
startpage = 'qthelp://' + posixpath.join(nspace, 'doc', 'index.html')

logger.info(__('writing collection project file...'))
with open(path.join(outdir, outname + '.qhcp'), 'w', # type: ignore
encoding='utf-8') as f:
with open(path.join(outdir, outname + '.qhcp'), 'w', encoding='utf-8') as f:
body = render_file('project.qhcp', outname=outname,
title=self.config.html_short_title,
homepage=homepage, startpage=startpage)
Expand Down
2 changes: 1 addition & 1 deletion sphinx/builders/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def write_doc(self, docname, doctree):
outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix)
ensuredir(path.dirname(outfilename))
try:
with open(outfilename, 'w', encoding='utf-8') as f: # type: ignore
with open(outfilename, 'w', encoding='utf-8') as f:
f.write(self.writer.output)
except (IOError, OSError) as err:
logger.warning(__("error writing file %s: %s"), outfilename, err)
Expand Down
2 changes: 1 addition & 1 deletion sphinx/builders/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def write_doc(self, docname, doctree):
outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix)
ensuredir(path.dirname(outfilename))
try:
with open(outfilename, 'w', encoding='utf-8') as f: # type: ignore
with open(outfilename, 'w', encoding='utf-8') as f:
f.write(self.writer.output)
except (IOError, OSError) as err:
logger.warning(__("error writing file %s: %s"), outfilename, err)
Expand Down
2 changes: 1 addition & 1 deletion sphinx/cmd/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,4 @@ def main(argv=sys.argv[1:]): # type: ignore


if __name__ == '__main__':
sys.exit(main(sys.argv[1:])) # type: ignore
sys.exit(main(sys.argv[1:]))
2 changes: 1 addition & 1 deletion sphinx/cmd/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def write_file(fpath, content, newline=None):
if overwrite or not path.isfile(fpath):
if 'quiet' not in d:
print(__('Creating file %s.') % fpath)
with open(fpath, 'wt', encoding='utf-8', newline=newline) as f: # type: ignore
with open(fpath, 'wt', encoding='utf-8', newline=newline) as f:
f.write(content)
else:
if 'quiet' not in d:
Expand Down
6 changes: 3 additions & 3 deletions sphinx/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def init_values(self):
logger.warning("%s", exc)
for name in config:
if name in self.values:
self.__dict__[name] = config[name] # type: ignore
self.__dict__[name] = config[name]

def __getattr__(self, name):
# type: (unicode) -> Any
Expand Down Expand Up @@ -306,7 +306,7 @@ def __contains__(self, name):
def __iter__(self):
# type: () -> Generator[ConfigValue, None, None]
for name, value in self.values.items():
yield ConfigValue(name, getattr(self, name), value[1]) # type: ignore
yield ConfigValue(name, getattr(self, name), value[1])

def add(self, name, default, rebuild, types):
# type: (unicode, Any, Union[bool, unicode], Any) -> None
Expand Down Expand Up @@ -334,7 +334,7 @@ def __getstate__(self):

# create a picklable copy of values list
__dict__['values'] = {}
for key, value in self.values.items(): # type: ignore
for key, value in self.values.items():
real_value = getattr(self, key)
if not is_serializable(real_value):
# omit unserializable value
Expand Down
3 changes: 1 addition & 2 deletions sphinx/directives/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ def parse_options(self):
def read_file(self, filename, location=None):
# type: (unicode, Any) -> List[unicode]
try:
with open(filename, # type: ignore
encoding=self.encoding, errors='strict') as f:
with open(filename, encoding=self.encoding, errors='strict') as f:
text = f.read() # type: unicode
if 'tab-width' in self.options:
text = text.expandtabs(self.options['tab-width'])
Expand Down
4 changes: 2 additions & 2 deletions sphinx/directives/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ def run(self):
meta = node.details['nodes'][0]
meta.source = self.env.doc2path(self.env.docname)
meta.line = self.lineno
meta.rawcontent = meta['content']
meta.rawcontent = meta['content'] # type: ignore

# docutils' meta nodes aren't picklable because the class is nested
meta.__class__ = addnodes.meta
meta.__class__ = addnodes.meta # type: ignore

return result

Expand Down
2 changes: 1 addition & 1 deletion sphinx/domains/changeset.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def note_changeset(self, node):
version = node['version']
module = self.env.ref_context.get('py:module')
objname = self.env.temp_data.get('object')
changeset = ChangeSet(node['type'], self.env.docname, node.line, # type: ignore
changeset = ChangeSet(node['type'], self.env.docname, node.line,
module, objname, node.astext())
self.data['changes'].setdefault(version, []).append(changeset)

Expand Down
4 changes: 2 additions & 2 deletions sphinx/domains/cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2232,7 +2232,7 @@ def describe_signature(self, signode, mode, env, symbol):
# so it can remove it in inner declarations.
dest = signode
if mode == 'lastIsName':
dest = addnodes.desc_addname()
dest = addnodes.desc_addname() # type: ignore
for i in range(len(names)):
nne = names[i]
template = self.templates[i]
Expand Down Expand Up @@ -6936,7 +6936,7 @@ def checkType():
# the non-identifier refs are cross-references, which should be processed:
# - fix parenthesis due to operator() and add_function_parentheses
if typ != "identifier":
title = contnode.pop(0).astext()
title = contnode.pop(0).astext() # type: ignore
# If it's operator(), we need to add '()' if explicit function parens
# are requested. Then the Sphinx machinery will add another pair.
# Also, if it's an 'any' ref that resolves to a function, we need to add
Expand Down
2 changes: 1 addition & 1 deletion sphinx/domains/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def make_xrefs(self,
delims_re = re.compile(delims)
sub_targets = re.split(delims, target)

split_contnode = bool(contnode and contnode.astext() == target)
split_contnode = bool(contnode and contnode.astext() == target) # type: ignore

results = []
for sub_target in filter(None, sub_targets):
Expand Down
2 changes: 1 addition & 1 deletion sphinx/environment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def relfn2path(self, filename, docname=None):
# the source directory is a bytestring with non-ASCII characters;
# let's try to encode the rel_fn in the file system encoding
enc_rel_fn = rel_fn.encode(sys.getfilesystemencoding())
return rel_fn, path.abspath(path.join(self.srcdir, enc_rel_fn))
return rel_fn, path.abspath(path.join(self.srcdir, enc_rel_fn)) # type: ignore

@property
def found_docs(self):
Expand Down
Loading