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

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

Open
OmarAlJarrah opened this issue Mar 31, 2023 · 0 comments

Comments

@OmarAlJarrah
Copy link
Collaborator

OmarAlJarrah commented Mar 31, 2023

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

@OmarAlJarrah OmarAlJarrah changed the title GoogleDocstrings: Issue Parsing constr with regex as Attributes. GoogleDocstrings: Issue Parsing constr with regex inside Attributes Section. Mar 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant