diff --git a/mkdocs/commands/build.py b/mkdocs/commands/build.py index 1fd31826..144c6230 100644 --- a/mkdocs/commands/build.py +++ b/mkdocs/commands/build.py @@ -136,7 +136,7 @@ def _build_extra_template(template_name, files, config, nav): return try: - with open(file.abs_src_path, 'r', encoding='utf-8', errors='strict') as f: + with open(file.abs_src_path, encoding='utf-8', errors='strict') as f: template = jinja2.Template(f.read()) except Exception as e: log.warning(f"Error reading template '{template_name}': {e}") diff --git a/mkdocs/commands/gh_deploy.py b/mkdocs/commands/gh_deploy.py index 59f81e2d..b6b80fb4 100644 --- a/mkdocs/commands/gh_deploy.py +++ b/mkdocs/commands/gh_deploy.py @@ -116,14 +116,14 @@ def gh_deploy(config, message=None, force=False, ignore_version=False, shell=Fal nojekyll=True ) except ghp_import.GhpError as e: - log.error("Failed to deploy to GitHub with error: \n{}".format(e.message)) + log.error(f"Failed to deploy to GitHub with error: \n{e.message}") raise Abort('Deployment Aborted!') cname_file = os.path.join(config['site_dir'], 'CNAME') # Does this repository have a CNAME set for GitHub pages? if os.path.isfile(cname_file): # This GitHub pages repository has a CNAME configured. - with(open(cname_file, 'r')) as f: + with open(cname_file) as f: cname_host = f.read().strip() log.info(f'Based on your CNAME file, your documentation should be ' f'available shortly at: http://{cname_host}') diff --git a/mkdocs/config/base.py b/mkdocs/config/base.py index f06e3c03..8f7f8bef 100644 --- a/mkdocs/config/base.py +++ b/mkdocs/config/base.py @@ -126,7 +126,7 @@ class Config(UserDict): raise exceptions.ConfigurationError( "The configuration is invalid. The expected type was a key " "value mapping (a python dict) but we got an object of type: " - "{}".format(type(patch))) + f"{type(patch)}") self.user_configs.append(patch) self.data.update(patch) @@ -234,11 +234,11 @@ def load_config(config_file=None, **kwargs): if len(errors) > 0: raise exceptions.Abort( - "Aborted with {} Configuration Errors!".format(len(errors)) + f"Aborted with {len(errors)} Configuration Errors!" ) elif cfg['strict'] and len(warnings) > 0: raise exceptions.Abort( - "Aborted with {} Configuration Warnings in 'strict' mode!".format(len(warnings)) + f"Aborted with {len(warnings)} Configuration Warnings in 'strict' mode!" ) return cfg diff --git a/mkdocs/config/config_options.py b/mkdocs/config/config_options.py index 025fd5a6..374d9619 100644 --- a/mkdocs/config/config_options.py +++ b/mkdocs/config/config_options.py @@ -147,9 +147,8 @@ class Type(OptionallyRequired): if not isinstance(value, self._type): msg = f"Expected type: {self._type} but received: {type(value)}" elif self.length is not None and len(value) != self.length: - msg = ("Expected type: {0} with length {2} but received: {1} with " - "length {3}").format(self._type, value, self.length, - len(value)) + msg = (f"Expected type: {self._type} with length {self.length}" + f" but received: {value} with length {len(value)}") else: return value @@ -274,10 +273,10 @@ class IpAddress(OptionallyRequired): host = config[key_name].host if key_name == 'dev_addr' and host in ['0.0.0.0', '::']: self.warnings.append( - ("The use of the IP address '{}' suggests a production environment " + (f"The use of the IP address '{host}' suggests a production environment " "or the use of a proxy to connect to the MkDocs server. However, " "the MkDocs' server is intended for local development purposes only. " - "Please use a third party production-ready server instead.").format(host) + "Please use a third party production-ready server instead.") ) @@ -392,9 +391,9 @@ class Dir(FilesystemObject): # Validate that the dir is not the parent dir of the config file. if os.path.dirname(config.config_file_path) == config[key_name]: raise ValidationError( - ("The '{0}' should not be the parent directory of the config " - "file. Use a child directory instead so that the '{0}' " - "is a sibling of the config file.").format(key_name)) + (f"The '{key_name}' should not be the parent directory of the" + " config file. Use a child directory instead so that the" + f" '{key_name}' is a sibling of the config file.")) class File(FilesystemObject): @@ -487,7 +486,7 @@ class Theme(BaseConfigOption): format(path=theme_config['custom_dir'], name=key_name)) if 'locale' in theme_config and not isinstance(theme_config['locale'], str): - raise ValidationError("'{name}.locale' must be a string.".format(name=theme_config['name'])) + raise ValidationError(f"'{theme_config['name']}.locale' must be a string.") config[key_name] = theme.Theme(**theme_config) @@ -647,9 +646,9 @@ class Plugins(OptionallyRequired): Plugin = self.installed_plugins[name].load() if not issubclass(Plugin, plugins.BasePlugin): - raise ValidationError('{}.{} must be a subclass of {}.{}'.format( - Plugin.__module__, Plugin.__name__, plugins.BasePlugin.__module__, - plugins.BasePlugin.__name__)) + raise ValidationError( + f'{Plugin.__module__}.{Plugin.__name__} must be a subclass of' + f' {plugins.BasePlugin.__module__}.{plugins.BasePlugin.__name__}') plugin = Plugin() errors, warnings = plugin.load_config(config, self.config_file_path) diff --git a/mkdocs/plugins.py b/mkdocs/plugins.py index 8cd60e2d..4cc978b9 100644 --- a/mkdocs/plugins.py +++ b/mkdocs/plugins.py @@ -76,9 +76,9 @@ class PluginCollection(OrderedDict): def __setitem__(self, key, value, **kwargs): if not isinstance(value, BasePlugin): raise TypeError( - '{}.{} only accepts values which are instances of {}.{} ' - 'sublcasses'.format(self.__module__, self.__name__, - BasePlugin.__module__, BasePlugin.__name__)) + f'{self.__module__}.{self.__name__} only accepts values which' + f' are instances of {BasePlugin.__module__}.{BasePlugin.__name__}' + ' subclasses') super().__setitem__(key, value, **kwargs) # Register all of the event methods defined for this Plugin. for event_name in (x for x in dir(value) if x.startswith('on_')): diff --git a/mkdocs/structure/files.py b/mkdocs/structure/files.py index 26264625..e42c05fc 100644 --- a/mkdocs/structure/files.py +++ b/mkdocs/structure/files.py @@ -142,8 +142,9 @@ class File: return not self.__eq__(other) def __repr__(self): - return "File(src_path='{}', dest_path='{}', name='{}', url='{}')".format( - self.src_path, self.dest_path, self.name, self.url + return ( + f"File(src_path='{self.src_path}', dest_path='{self.dest_path}'," + f" name='{self.name}', url='{self.url}')" ) def _get_stem(self): diff --git a/mkdocs/structure/nav.py b/mkdocs/structure/nav.py index dac1beef..b8883773 100644 --- a/mkdocs/structure/nav.py +++ b/mkdocs/structure/nav.py @@ -127,18 +127,17 @@ def get_navigation(files, config): scheme, netloc, path, query, fragment = urlsplit(link.url) if scheme or netloc: log.debug( - "An external link to '{}' is included in " - "the 'nav' configuration.".format(link.url) + f"An external link to '{link.url}' is included in the 'nav' configuration." ) elif link.url.startswith('/'): log.debug( - "An absolute path to '{}' is included in the 'nav' configuration, " - "which presumably points to an external resource.".format(link.url) + f"An absolute path to '{link.url}' is included in the 'nav' " + "configuration, which presumably points to an external resource." ) else: msg = ( - "A relative path to '{}' is included in the 'nav' configuration, " - "which is not found in the documentation files".format(link.url) + f"A relative path to '{link.url}' is included in the 'nav' " + "configuration, which is not found in the documentation files" ) log.warning(msg) return Navigation(items, pages) diff --git a/mkdocs/structure/pages.py b/mkdocs/structure/pages.py index 76eede6a..b993ae24 100644 --- a/mkdocs/structure/pages.py +++ b/mkdocs/structure/pages.py @@ -54,7 +54,8 @@ class Page: def __repr__(self): title = f"'{self.title}'" if (self.title is not None) else '[blank]' - return "Page(title={}, url='{}')".format(title, self.abs_url or self.file.url) + url = self.abs_url or self.file.url + return f"Page(title={title}, url='{url}')" def _indent_print(self, depth=0): return '{}{}'.format(' ' * depth, repr(self)) @@ -116,7 +117,7 @@ class Page: ) if source is None: try: - with open(self.file.abs_src_path, 'r', encoding='utf-8-sig', errors='strict') as f: + with open(self.file.abs_src_path, encoding='utf-8-sig', errors='strict') as f: source = f.read() except OSError: log.error(f'File not found: {self.file.src_path}') diff --git a/mkdocs/tests/gh_deploy_tests.py b/mkdocs/tests/gh_deploy_tests.py index f2301df8..f9fa5491 100644 --- a/mkdocs/tests/gh_deploy_tests.py +++ b/mkdocs/tests/gh_deploy_tests.py @@ -159,7 +159,7 @@ class TestGitHubDeployLogs(unittest.TestCase): gh_deploy._check_version('gh-pages') self.assertEqual( cm.output, ['INFO:mkdocs.commands.gh_deploy:Previous deployment was done with MkDocs ' - 'version 0.1.2; you are deploying with a newer version ({})'.format(__version__)] + f'version 0.1.2; you are deploying with a newer version ({__version__})'] ) @mock.patch('subprocess.Popen') @@ -171,8 +171,8 @@ class TestGitHubDeployLogs(unittest.TestCase): self.assertRaises(Abort, gh_deploy._check_version, 'gh-pages') self.assertEqual( cm.output, ['ERROR:mkdocs.commands.gh_deploy:Deployment terminated: Previous deployment was made with ' - 'MkDocs version 10.1.2; you are attempting to deploy with an older version ({}). Use ' - '--ignore-version to deploy anyway.'.format(__version__)] + f'MkDocs version 10.1.2; you are attempting to deploy with an older version ({__version__}).' + ' Use --ignore-version to deploy anyway.'] ) @mock.patch('subprocess.Popen') diff --git a/mkdocs/tests/localization_tests.py b/mkdocs/tests/localization_tests.py index 704c7989..a7ceff96 100644 --- a/mkdocs/tests/localization_tests.py +++ b/mkdocs/tests/localization_tests.py @@ -1,7 +1,5 @@ #!/usr/bin/env python -# coding: utf-8 -from __future__ import unicode_literals import unittest diff --git a/mkdocs/tests/plugin_tests.py b/mkdocs/tests/plugin_tests.py index 295a75d1..bc40a296 100644 --- a/mkdocs/tests/plugin_tests.py +++ b/mkdocs/tests/plugin_tests.py @@ -21,7 +21,7 @@ class DummyPlugin(plugins.BasePlugin): def on_pre_page(self, content, **kwargs): """ modify page content by prepending `foo` config value. """ - return '{} {}'.format(self.config['foo'], content) + return f'{self.config["foo"]} {content}' def on_nav(self, item, **kwargs): """ do nothing (return None) to not modify item. """ @@ -29,7 +29,7 @@ class DummyPlugin(plugins.BasePlugin): def on_page_read_source(self, **kwargs): """ create new source by prepending `foo` config value to 'source'. """ - return '{} {}'.format(self.config['foo'], 'source') + return f'{self.config["foo"]} source' def on_pre_build(self, **kwargs): """ do nothing (return None). """ diff --git a/mkdocs/tests/search_tests.py b/mkdocs/tests/search_tests.py index 439faea7..c0cc0e1e 100644 --- a/mkdocs/tests/search_tests.py +++ b/mkdocs/tests/search_tests.py @@ -382,7 +382,7 @@ class SearchIndexTests(unittest.TestCase): """) toc = get_toc(get_markdown_toc(md)) - full_content = ''.join("""Heading{0}Content{0}""".format(i) for i in range(1, 4)) + full_content = ''.join(f"Heading{i}Content{i}" for i in range(1, 4)) plugin = search.SearchPlugin() errors, warnings = plugin.load_config({}) diff --git a/mkdocs/tests/structure/file_tests.py b/mkdocs/tests/structure/file_tests.py index b1b399a0..6ecc2587 100644 --- a/mkdocs/tests/structure/file_tests.py +++ b/mkdocs/tests/structure/file_tests.py @@ -650,7 +650,7 @@ class TestFiles(PathAssertionMixin, unittest.TestCase): dest_path = os.path.join(dest_dir, 'test.txt') file.copy_file(dirty=False) self.assertPathIsFile(dest_path) - with open(dest_path, 'r', encoding='utf-8') as f: + with open(dest_path, encoding='utf-8') as f: self.assertEqual(f.read(), 'source content') @tempdir(files={'test.txt': 'destination content'}) @@ -661,7 +661,7 @@ class TestFiles(PathAssertionMixin, unittest.TestCase): dest_path = os.path.join(dest_dir, 'test.txt') file.copy_file(dirty=True) self.assertPathIsFile(dest_path) - with open(dest_path, 'r', encoding='utf-8') as f: + with open(dest_path, encoding='utf-8') as f: self.assertEqual(f.read(), 'source content') @tempdir(files={'test.txt': 'destination content'}) @@ -672,7 +672,7 @@ class TestFiles(PathAssertionMixin, unittest.TestCase): dest_path = os.path.join(dest_dir, 'test.txt') file.copy_file(dirty=True) self.assertPathIsFile(dest_path) - with open(dest_path, 'r', encoding='utf-8') as f: + with open(dest_path, encoding='utf-8') as f: self.assertEqual(f.read(), 'destination content') def test_files_append_remove_src_paths(self): diff --git a/setup.py b/setup.py index b9834e51..e913eb1e 100755 --- a/setup.py +++ b/setup.py @@ -40,7 +40,8 @@ if sys.argv[-1] == 'publish': os.system("python setup.py sdist bdist_wheel") os.system("twine upload dist/*") print("You probably want to also tag the version now:") - print(" git tag -a {0} -m 'version {0}'".format(get_version("mkdocs"))) + version = get_version("mkdocs") + print(f" git tag -a {version} -m 'version {version}'") print(" git push --tags") sys.exit()