Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mypy typing error
#1
    @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!
Reply
#2
when posting errors, please, always post the entire, unaltered error traceback as it contains valuable diagnostic information.
WannaBePythonDev likes this post
Reply
#3
(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.

Error:
{ "resource": "~/Intro to Logic/Projects/propositions/syntax.py", "owner": "python", "code": "error", "severity": 8, "message": "Argument 1 to \"formula_obj_to_string\" of \"Formula\" has incompatible type \"Optional[Formula]\"; expected \"Formula\"", "source": "mypy", "startLineNumber": 134, "startColumn": 61, "endLineNumber": 134, "endColumn": 61 } { "resource": "~/Intro to Logic/Projects/propositions/syntax.py", "owner": "python", "code": "error", "severity": 8, "message": "Argument 2 to \"create_binary_formula\" of \"Formula\" has incompatible type \"Optional[Formula]\"; expected \"Formula\"", "source": "mypy", "startLineNumber": 175, "startColumn": 31, "endLineNumber": 175, "endColumn": 31 } { "resource": "~/Intro to Logic/Projects/propositions/syntax.py", "owner": "python", "code": "error", "severity": 8, "message": "Argument 3 to \"create_binary_formula\" of \"Formula\" has incompatible type \"Optional[Formula]\"; expected \"Formula\"", "source": "mypy", "startLineNumber": 175, "startColumn": 46, "endLineNumber": 175, "endColumn": 46 }
    @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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  mypy, type aliases and type variables tomciodev 1 657 Oct-18-2023, 10:08 AM
Last Post: Larz60+
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  problem in using mypy akbarza 17 2,739 Sep-06-2023, 01:53 PM
Last Post: snippsat
  Pywinauto typing in the wrong field EGameiro 0 656 Jun-07-2023, 10:01 PM
Last Post: EGameiro
  Typing and variable initializers jgossage 0 1,605 May-29-2020, 03:18 PM
Last Post: jgossage
  Need Help Typing Text into Tough Form [xpath / selenium] digitalmatic7 0 1,731 Jun-05-2019, 06:46 AM
Last Post: digitalmatic7
  Is there any way to simulate clicks/typing to an inactive window using Python? Nwb 1 5,852 Jun-11-2018, 08:56 AM
Last Post: Larz60+
  Typing into CMD Prompt while its behind another window andy410 0 2,510 Apr-27-2018, 08:24 PM
Last Post: andy410

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020