Skip to content

extract_condition

Create condition object given head token of condition phrase in docstring.

Source code in library_analyzer/processing/dependencies/_get_dependency.py
def extract_condition(condition_token: Token) -> Condition:
    """
    Create condition object given head token of condition phrase in docstring.
    """
    condition_token_subtree = list(condition_token.subtree)
    condition_text = " ".join([token.text for token in condition_token_subtree])

    is_none_phrases = [
        "is none",
        "is also none" "is not set",
        "is not specified",
        "is not none",
        "if none",
        "if not none",
    ]
    has_value_phrases = [
        "equals",
        "is true",
        "is false",
        "is set to",
        "is greater than",
        "is less than",
    ]
    if any(phrase in condition_text.lower() for phrase in is_none_phrases):
        return ParameterIsNone(condition=condition_text)
    elif any(phrase in condition_text.lower() for phrase in has_value_phrases):
        return ParameterHasValue(condition=condition_text)
    else:
        return Condition(condition=condition_text)