Skip to content

GoogleDocstrings: Issue Parsing constr with regex inside Attributes Section. #76

@OmarAlJarrah

Description

@OmarAlJarrah

Description

When an attribute in the docstrings has a constr in its type hint, with a regex as a constraint, the splitting of type and description is not correct.

Consider the following docstring:

Atttributes:
    variable(Optional[constr(max_length=6, regex=r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|^(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}$')], optional): description.

If we take a look on how splitting of description and anything that comes before it, it happens as follows (in google.py, line 115):

# Split spec and description
before, desc = text.split(":", 1)

As we can see, we are splitting depending on the first colon : that occurs in the string, which results in the issue, given the first occurrence of a colon is in the regex.

Suggestions

I have suffered from the issue, then I started developing a solution to properly cover this edge case, it is very simple considering the issue itself is not.

Solution

Adding a simple if-statement, to check if regex= is present in the before part, and validate wether the type hint in the before part is actually a valid type hint, using ast, astor and astvalidate, building a type hint ast node, and validating its integrity, then using some regexes to do a further better parsing if needed, and solve the problem.

Far More Improvement:

Consider the previous solution as a solution to the problem we have, I think having a set of Validator objects, where each object is just a wrapper around a Callable, and a list of fixers, where each fixer is a Callable that attemps_fix the issue for it's wrapper validator, would help solve as many similar issues as possible, without the need to mess up the code.

Final Words

Developing the Far More Improved version would take time and effort, plus few discussions to agree on the design of such solution, such that it works properly for all parsers, for now I suggest we simply go ahead and solve the issue, if we agree here, I can push a PR to fix the problem, as I have already developed the solution locally.

Thank You

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions