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

[jsonschema.py:_field_docs_for] Regex gets incorrect matches #13

Open
mickmis opened this issue Feb 2, 2024 · 0 comments
Open

[jsonschema.py:_field_docs_for] Regex gets incorrect matches #13

mickmis opened this issue Feb 2, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@mickmis
Copy link

mickmis commented Feb 2, 2024

(initially reported here)

The doc_pattern regex is supposed (IIUC) to capture the docstrings of the class attributes:

doc_pattern = r"\n\s+([_a-zA-Z][_a-zA-Z0-9]*)(?:: [^\n]+)?\n(\s+)(?:\"\"\"|''')((?:.|\s)*?)(?:\"\"\"|''')"

However some docstrings specifically formed might be incorrectly captured by this regex, which results in the call to finditer to hang indefinitely.

Here is a minimal example reproducing this issue:

class Query(ImplicitDict):

    participant_id: Optional[str]
    """If specified, identifier of the USS/participant hosting the server involved in this query."""

    def parse_json_result(self, parse_type: Type[ResponseType]) -> ResponseType:
        """Parses the JSON result into the specified type.

        Args:
            parse_type: ImplicitDict type to parse into.
        Returns:
             the parsed response (of type `parse_type`).
        Raises:
            QueryError: if the parsing failed.
        """
        try:
            return parse_type(ImplicitDict.parse(self.response.json, parse_type))
        except (ValueError, TypeError, KeyError) as e:
            raise QueryError(
                f"Parsing JSON response into type {parse_type.__name__} failed with exception {type(e).__name__}: {e}",
                self,
            )


class QueryError(RuntimeError):
    """Error encountered when interacting with a server in the UTM ecosystem."""

    queries: List[Query]

A workaround in the previous example would be to add e.g. a * before QueryError.

This issue can be easily highlighted using e.g. https://regex101.com and:

  • selecting Python flavor
  • copying the above example as the test string
  • copying the faulty regex:
\n\s+([_a-zA-Z][_a-zA-Z0-9]*)(?:: [^\n]+)?\n(\s+)(?:\"\"\"|''')((?:.|\s)*?)(?:\"\"\"|''')
  • we then see that participant_id is correctly caught, while QueryError is incorrectly caught as a class attribute with a docstring
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant