diff --git a/docs/hooks.py b/docs/hooks.py index 1ab58b88..34b556f1 100644 --- a/docs/hooks.py +++ b/docs/hooks.py @@ -16,7 +16,7 @@ def _get_language_of_translation_file(path: Path) -> str: def on_page_markdown(markdown: str, page: Page, config: MkDocsConfig, **kwargs): if page.file.src_uri == 'user-guide/choosing-your-theme.md': - here = Path(config.config_file_path or '').parent + here = Path(config.config_file_path).parent def replacement(m: re.Match) -> str: lines = [] diff --git a/mkdocs/commands/gh_deploy.py b/mkdocs/commands/gh_deploy.py index de5fb99b..f6e5fa05 100644 --- a/mkdocs/commands/gh_deploy.py +++ b/mkdocs/commands/gh_deploy.py @@ -116,7 +116,7 @@ def gh_deploy( if message is None: message = default_message - sha = _get_current_sha(os.path.dirname(config.config_file_path or '')) + sha = _get_current_sha(os.path.dirname(config.config_file_path)) message = message.format(version=mkdocs.__version__, sha=sha) log.info( diff --git a/mkdocs/commands/serve.py b/mkdocs/commands/serve.py index 48095f9b..1797e815 100644 --- a/mkdocs/commands/serve.py +++ b/mkdocs/commands/serve.py @@ -100,7 +100,8 @@ def serve( if livereload: # Watch the documentation files, the config file and the theme files. server.watch(config.docs_dir) - server.watch(config.config_file_path) + if config.config_file_path: + server.watch(config.config_file_path) if watch_theme: for d in config.theme.dirs: diff --git a/mkdocs/config/base.py b/mkdocs/config/base.py index dcbc6e23..14f1d953 100644 --- a/mkdocs/config/base.py +++ b/mkdocs/config/base.py @@ -133,7 +133,7 @@ class Config(UserDict): """ _schema: PlainConfigSchema - config_file_path: str | None + config_file_path: str def __init_subclass__(cls): schema = dict(getattr(cls, '_schema', ())) @@ -170,7 +170,7 @@ class Config(UserDict): config_file_path = config_file_path.decode(encoding=sys.getfilesystemencoding()) except UnicodeDecodeError: raise ValidationError("config_file_path is not a Unicode string.") - self.config_file_path = config_file_path + self.config_file_path = config_file_path or '' def set_defaults(self) -> None: """ @@ -344,7 +344,9 @@ def _open_config_file(config_file: str | IO | None) -> Iterator[IO]: result_config_file.close() -def load_config(config_file: str | IO | None = None, **kwargs) -> MkDocsConfig: +def load_config( + config_file: str | IO | None = None, *, config_file_path: str | None = None, **kwargs +) -> MkDocsConfig: """ Load the configuration for a given file object or name @@ -366,7 +368,10 @@ def load_config(config_file: str | IO | None = None, **kwargs) -> MkDocsConfig: # Initialize the config with the default schema. from mkdocs.config.defaults import MkDocsConfig - cfg = MkDocsConfig(config_file_path=getattr(fd, 'name', '')) + if config_file_path is None: + if fd is not sys.stdin.buffer: + config_file_path = getattr(fd, 'name', None) + cfg = MkDocsConfig(config_file_path=config_file_path) # load the config file cfg.load_file(fd) diff --git a/mkdocs/config/config_options.py b/mkdocs/config/config_options.py index 23a113c0..05994b03 100644 --- a/mkdocs/config/config_options.py +++ b/mkdocs/config/config_options.py @@ -708,7 +708,7 @@ class Dir(FilesystemObject): class DocsDir(Dir): def post_validation(self, config: Config, key_name: str): - if config.config_file_path is None: + if not config.config_file_path: return # Validate that the dir is not the parent dir of the config file. @@ -826,7 +826,7 @@ class Theme(BaseConfigOption[theme.Theme]): # Ensure custom_dir is an absolute path if 'custom_dir' in theme_config and not os.path.isabs(theme_config['custom_dir']): - config_dir = os.path.dirname(self.config_file_path or '') + config_dir = os.path.dirname(self.config_file_path) theme_config['custom_dir'] = os.path.join(config_dir, theme_config['custom_dir']) if 'custom_dir' in theme_config and not os.path.isdir(theme_config['custom_dir']): diff --git a/mkdocs/config/defaults.py b/mkdocs/config/defaults.py index 891ee710..71e01bcb 100644 --- a/mkdocs/config/defaults.py +++ b/mkdocs/config/defaults.py @@ -16,7 +16,7 @@ def get_schema() -> base.PlainConfigSchema: class MkDocsConfig(base.Config): """The configuration of MkDocs itself (the root object of mkdocs.yml).""" - config_file_path: str | None = c.Optional(c.Type(str)) # type: ignore[assignment] + config_file_path: str = c.Type(str) # type: ignore[assignment] """The path to the mkdocs.yml config file. Can't be populated from the config.""" site_name = c.Type(str)