Mypy typing error - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Mypy typing error (/thread-34828.html) |
Mypy typing error - WannaBePythonDev - Sep-04-2021 @staticmethod def formula_obj_to_string(formula: Formula) -> str: """Recursive function to assemble a string representation of the formula described by the a Formula object. Parameters: formula: The Formula object to build the string repr. of. Returns: A string representing the formula object. """ if is_variable(formula.root) or is_constant(formula.root): return formula.root return ( Formula.negate_formula(formula) if is_unary(formula.root) else Formula.create_binary_formula( formula.root, formula.first, formula.second ) ) class Formula: """An immutable propositional formula in tree representation, composed from atomic propositions, and operators applied to them. Attributes: root (`str`): the constant, atomic proposition, or operator at the root of the formula tree. first (`~typing.Optional`\\[`Formula`]): the first operand to the root, if the root is a unary or binary operator. second (`~typing.Optional`\\[`Formula`]): the second operand to the root, if the root is a binary operator. """ root: str first: Optional[Formula] second: Optional[Formula] @lru_cache(maxsize=100) # Cache the return value of is_unary def is_unary(string: str) -> bool: """Checks if the given string is a unary operator. Parameters: string: string to check. Returns: ``True`` if the given string is a unary operator, ``False`` otherwise. """ return string == "~" Mypy gives me an error: Argument 1 to "formula_obj_to_string" of "Formula" has incompatible type "Optional[Formula]"; expected "Formula" But in this case, I know that according to the conditions that the argument formula.first will be of type Formula . How can I type-assert it here so that Mypy won't yell on that?Thanks in advance! RE: Mypy typing error - Larz60+ - Sep-04-2021 when posting errors, please, always post the entire, unaltered error traceback as it contains valuable diagnostic information. RE: Mypy typing error - WannaBePythonDev - Sep-05-2021 (Sep-04-2021, 10:08 PM)Larz60+ Wrote: when posting errors, please, always post the entire, unaltered error traceback as it contains valuable diagnostic information.
@staticmethod def negate_formula(formula: Formula) -> str: """Negates a formula. Parameters: formula: A formula to negate. Returns: A string reresenting the formula negated. """ return formula.root + Formula.formula_obj_to_string(formula.first) @staticmethod def create_binary_formula( binary_operator: str, first_sub_formula: Formula, second_sub_formula: Formula ) -> str: """Assmebles a binary formula out of two sub-formulas. Parameters: binary_operator: The binary operator applied on the two sub-formulas. first_sub_formula: The first sub-formula. second_sub_formula: The second sub-formula. Returns: A string reresenting the suitable binary formula. """ return ( OPEN_BINARY_FORM + Formula.formula_obj_to_string(first_sub_formula) + binary_operator + Formula.formula_obj_to_string(second_sub_formula) + CLOSE_BINARY_FORM )This is all the related stuff. |