rstcheck_core package
Submodules
rstcheck_core.checker module
- rstcheck_core.checker.check_file(source_file, rstcheck_config, overwrite_with_file_config=True)[source]
Check the given file for issues.
On every call docutils’ caches for roles and directives are cleared by reloading their modules.
- Parameters:
source_file (
Path) – Path to file to checkrstcheck_config (
RstcheckConfig) – Main configuration of the applicationoverwrite_with_file_config (
bool) – If the loaded file config should overwrite therstcheck_config; defaults toTrue
- Return type:
- Returns:
A list of found issues
- rstcheck_core.checker.check_source(source, source_file=None, ignores=None, report_level=ReportLevel.INFO, *, warn_unknown_settings=False)[source]
Check the given rst source for issues.
- Parameters:
source_file (
Path|Literal['<string>','<stdin>'] |None) – Path to file the source comes from if it comes from a file; defaults toNoneignores (
IgnoreDict|None) – Ignore information; defaults toNonereport_level (
ReportLevel) – Report level; defaults torstcheck_core.config.DEFAULT_REPORT_LEVELwarn_unknown_settings (
bool) – If a warning should be logged for unknown settings in config file; defaults toFalsesource (str)
- Returns:
- Yield:
Found issues
- Return type:
- class rstcheck_core.checker.CodeBlockChecker(source_origin, ignores=None, report_level=ReportLevel.INFO, *, warn_unknown_settings=False)[source]
Bases:
objectChecker for code blocks with different languages.
Initialize CodeBlockChecker.
- Parameters:
source_origin (
Path|Literal['<string>','<stdin>']) – Path to file the source comes fromignores (
IgnoreDict|None) – Ignore information; defaults toNonereport_level (
ReportLevel) – Report level; defaults torstcheck_core.config.DEFAULT_REPORT_LEVELwarn_unknown_settings (
bool) – If a warning should be logged for unknown settings in config file; defaults toFalse
- create_checker(source_code, language)[source]
Create a checker function for the given source and language.
- check(source_code, language)[source]
Call the appropriate checker function for the given language to check given source.
- check_doctest(source_code)[source]
Check doctest source for syntax errors.
This does not run the test as that would be unsafe. Nor does this check the Python syntax in the doctest. That could be purposely incorrect for testing purposes.
rstcheck_core.config module
- rstcheck_core.config.CONFIG_FILES = ['.rstcheck.cfg', 'pyproject.toml', 'setup.cfg']
Supported default config files.
- class rstcheck_core.config.ReportLevel(*values)[source]
Bases:
EnumReport levels supported by docutils.
- INFO = 1
- WARNING = 2
- ERROR = 3
- SEVERE = 4
- NONE = 5
- rstcheck_core.config.ReportLevelMap = {'error': 3, 'info': 1, 'none': 5, 'severe': 4, 'warning': 2}
Map docutils report levels in text form to numbers.
- rstcheck_core.config.DEFAULT_REPORT_LEVEL = ReportLevel.INFO
Default report level.
- class rstcheck_core.config.RstcheckConfigFile(**data)[source]
Bases:
BaseModelRstcheck config file.
- Raises:
ValueError – If setting has incorrect value or type
pydantic.ValidationError – If setting is not parsable into correct type
- Parameters:
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- report_level: ReportLevel | None
- classmethod valid_report_level(value)[source]
Validate the report_level setting.
- Parameters:
value (
Any) – Value to validate- Raises:
ValueError – If
valueis not a valid docutils report level- Return type:
- Returns:
Instance of
ReportLevelor None if empty string.
- classmethod split_str(value)[source]
Validate and parse the following ignore_* settings.
ignore_directives
ignore_roles
ignore_substitutions
ignore_languages
Comma separated strings are split into a list.
- classmethod join_regex_str(value)[source]
Validate and concatenate the ignore_messages setting to a RegEx string.
If a list is given, the entries are concatenated with “|” to create an or RegEx.
- Parameters:
value (
Any) – Value to validate- Raises:
ValueError – If not a
strorlistofstr- Return type:
- Returns:
A RegEx string with messages to ignore or
typing.Patternif it is one already
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class rstcheck_core.config.RstcheckConfig(**data)[source]
Bases:
RstcheckConfigFileRstcheck config.
- Raises:
ValueError – If setting has incorrect value or type
pydantic.ValidationError – If setting is not parsable into correct type
- Parameters:
report_level (ReportLevel | None)
config_path (Path | None)
recursive (bool | None)
warn_unknown_settings (bool | None)
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- rstcheck_core.config.load_config_file(file_path, *, log_missing_section_as_warning=True, warn_unknown_settings=False)[source]
Load, parse and validate rstcheck config from a file.
Caution
If a TOML file is passed this function need tomli installed for python versions before 3.11! Use toml extra or install manually.
- Parameters:
file_path (
Path) – File to load config fromlog_missing_section_as_warning (
bool) – If a missing config section should be logged at WARNING (True) orINFO(False) level; defaults toTruewarn_unknown_settings (
bool) – If a warning should be logged for unknown settings in config file; defaults toFalse
- Raises:
FileNotFoundError – If the file is not found
- Return type:
- Returns:
instance of
RstcheckConfigFileorNoneon missing config section orNONEis passed as the config path.
- rstcheck_core.config.load_config_file_from_dir(dir_path, *, log_missing_section_as_warning=False, warn_unknown_settings=False)[source]
Search, load, parse and validate rstcheck config from a directory.
Searches files from
CONFIG_FILESin the directory. If a file is found, try to load the config from it. If is has no config, search further.- Parameters:
dir_path (
Path) – Directory to searchlog_missing_section_as_warning (
bool) – If a missing config section in a config file should be logged at WARNING (True) orINFO(False) level; defaults toFalsewarn_unknown_settings (
bool) – If a warning should be logged for unknown settings in config file; defaults toFalse
- Return type:
- Returns:
instance of
RstcheckConfigFileorNoneif no file is found or no file has a rstcheck section orNONEis passed as the config path.
- rstcheck_core.config.load_config_file_from_dir_tree(dir_path, *, log_missing_section_as_warning=False, warn_unknown_settings=False)[source]
Search, load, parse and validate rstcheck config from a directory tree.
Searches files from
CONFIG_FILESin the directory. If a file is found, try to load the config from it. If is has no config, search further. If no config is found in the directory search its parents one by one.- Parameters:
dir_path (
Path) – Directory to searchlog_missing_section_as_warning (
bool) – If a missing config section in a config file should be logged atWARNING(True) orINFO(False) level; defaults toFalsewarn_unknown_settings (
bool) – If a warning should be logged for unknown settings in config file; defaults toFalse
- Return type:
- Returns:
instance of
RstcheckConfigFileorNoneif no file is found or no file has a rstcheck section orNONEis passed as the config path.
- rstcheck_core.config.load_config_file_from_path(path, *, search_dir_tree=False, log_missing_section_as_warning_for_file=True, log_missing_section_as_warning_for_dir=False, warn_unknown_settings=False)[source]
Analyze the path and call the correct config file loader.
- Parameters:
path (
Path) – Path to load config file from; can be a file or directorysearch_dir_tree (
bool) – If the directory tree should be searched; only applies ifpathis a directory; defaults toFalselog_missing_section_as_warning_for_file (
bool) – If a missing config section in a config file should be logged at WARNING (True) orINFO(False) level when the given path is a file; defaults toTruelog_missing_section_as_warning_for_dir (
bool) – If a missing config section in a config file should be logged atWARNING(True) orINFO(False) level when the given file is a directory; defaults toFalsewarn_unknown_settings (
bool) – If a warning should be logged for unknown settings in config file; defaults toFalse
- Raises:
FileNotFoundError – When the passed path is not found.
- Return type:
- Returns:
instance of
RstcheckConfigFileorNoneif no file is found or no file has a rstcheck section orNONEis passed as the config path.
- rstcheck_core.config.merge_configs(config_base, config_add, *, config_add_is_dominant=True)[source]
Merge two configs into a new one.
- Parameters:
config_base (
RstcheckConfig) – The base config to merge intoconfig_add (
RstcheckConfig|RstcheckConfigFile) – The config that is merged into theconfig_baseconfig_add_is_dominant (
bool) – If theconfig_addoverwrites values ofconfig_base; defaults toTrue
- Return type:
- Returns:
New merged config
rstcheck_core.inline_config module
- rstcheck_core.inline_config.get_inline_config_from_source(source, source_origin, *, warn_unknown_settings=False)[source]
Get rstcheck inline configs from source.
Unknown configs are ignored.
- Parameters:
- Return type:
- Returns:
A list of inline configs
- rstcheck_core.inline_config.find_ignored_directives(source, source_origin, *, warn_unknown_settings=False)[source]
Search the rst source for rstcheck inline ignore-directives comments.
Directives are ignored via comment.
For example, to ignore directive1, directive2, and directive3:
>>> list(find_ignored_directives(''' ... Example ... ======= ... ... .. rstcheck: ignore-directives=directive1,directive3 ... ... .. rstcheck: ignore-directives=directive2 ... ''', "<string>")) ['directive1', 'directive3', 'directive2']
- rstcheck_core.inline_config.find_ignored_roles(source, source_origin, *, warn_unknown_settings=False)[source]
Search the rst source for rstcheck inline ignore-roles comments.
Roles are ignored via comment.
For example, to ignore role1, role2, and role3:
>>> list(find_ignored_roles(''' ... Example ... ======= ... ... .. rstcheck: ignore-roles=role1,role3 ... ... .. rstcheck: ignore-roles=role2 ... ''', "<string>")) ['role1', 'role3', 'role2']
- rstcheck_core.inline_config.find_ignored_substitutions(source, source_origin, *, warn_unknown_settings=False)[source]
Search the rst source for rstcheck inline ignore-substitutions comments.
Substitutions are ignored via comment.
For example, to ignore substitution1, substitution2, and substitution3:
>>> list(find_ignored_substitutions(''' ... Example ... ======= ... ... .. rstcheck: ignore-substitutions=substitution1,substitution3 ... ... .. rstcheck: ignore-substitutions=substitution2 ... ''', "<string>")) ['substitution1', 'substitution3', 'substitution2']
- rstcheck_core.inline_config.find_ignored_languages(source, source_origin, *, warn_unknown_settings=False)[source]
Search the rst source for rstcheck inline ignore-languages comments.
Languages are ignored via comment.
For example, to ignore C++, JSON, and Python:
>>> list(find_ignored_languages(''' ... Example ... ======= ... ... .. rstcheck: ignore-languages=cpp,json ... ... .. rstcheck: ignore-languages=python ... ''', "<string>")) ['cpp', 'json', 'python']
- rstcheck_core.inline_config.get_inline_flow_control_from_source(source, source_origin, *, warn_unknown_settings=False)[source]
Get rstcheck inline flow control from source.
Unknown flow controls are ignored.
- Parameters:
- Return type:
- Returns:
A list of inline flow controls
rstcheck_core.runner module
- class rstcheck_core.runner.RstcheckMainRunner(check_paths, rstcheck_config, *, overwrite_config=True)[source]
Bases:
objectMain runner of rstcheck_core.
Initialize the
RstcheckMainRunnerwith a base config.- Parameters:
rstcheck_config (
RstcheckConfig) – Base configuration config from e.g. the CLI.overwrite_config (
bool) – If file config overwrites current config; defaults to True
- property files_to_check: list[Path]
List of files to check.
This list is updated via the
RstcheckMainRunner.update_file_list()method.
- property nonexisting_paths: list[Path]
List of paths which do not exist.
This list is updated via the
RstcheckMainRunner.update_file_list()method.
- load_config_file(config_path, *, warn_unknown_settings=False)[source]
Load config from file and merge with current config.
If the loaded file config overwrites the current config depends on the
self.overwrite_configattribute set on initialization.
- update_file_list()[source]
Update file path list with paths specified on initialization.
Uses paths from
RstcheckMainRunner.check_paths, resolves all file paths and saves them inRstcheckMainRunner.files_to_check.If a given path does not exist, it is filtered out and saved in
RstcheckMainRunner.files_to_check.Clear the current file list. Then get the file and directory paths specified with
self.check_pathsattribute set on initialization and search them for rst files to check. Add those files to the file list.- Return type:
- check()[source]
Check all files in the file list and save the errors.
Multiple files are run in parallel.
A new call overwrite the old cached errors.
- Return type:
rstcheck_core.types module
- rstcheck_core.types.SourceFileOrString = pathlib.Path | typing.Literal['<string>', '<stdin>']
Path to source file or if it is a string then ‘<string>’ or ‘<stdin>’.
- class rstcheck_core.types.LintError[source]
Bases:
TypedDictDict with information about an linting error.
- rstcheck_core.types.construct_ignore_dict(messages=None, languages=None, directives=None, roles=None, substitutions=None)[source]
Create an
IgnoreDictwith passed values or defaults.- Parameters:
messages (
Pattern[str] |None) – Value forIgnoreDict.messages;Noneresults in an empty list; defaults toNonedirectives (
list[str] |None) – Value forIgnoreDict.directives;Noneresults in an empty list; defaults toNoneroles (
list[str] |None) – Value forIgnoreDict.roles;Noneresults in an empty list; defaults toNonesubstitutions (
list[str] |None) – Value forIgnoreDict.substitutions;Noneresults in an empty list; defaults toNone
- Return type:
- Returns:
IgnoreDictwith passed values or defaults
- rstcheck_core.types.CheckerRunFunction
Function to run checks.
Returned by
rstcheck_core.checker.CodeBlockChecker.create_checker().