开发者

Python coding errors are not caught in linter or nor during execution rather they are thrown only when it is mapped or class methods invoked

I am relatively new to Python. Using Python 3.7 in this example below. The Linter is not catching any of the coding errors nor, it throws any exception when wrong return types are returned. What is the best and formal way of handing such issues?

from typing import Tuple
from abc import ABC, abstractmethod


class MyAbc(ABC):

    @abstractmethod
    def get_hello(self) -> Tuple[bool, str, str]:
        # Need the return to be a Tuple of 3 values: bool开发者_StackOverflow中文版, str, str
        pass


class ImplementedClass(MyAbc):
    def get_hello(self):
        return True, "Hello"
        # But in the implementation I am returning only 2 values: bool, str
        # This coding error is not caught here


ic: MyAbc = ImplementedClass()
print(ic.get_hello()) # Error escaped

resp1, resp2, resp3 = ic.get_hello()
# The issue is caught only here
# Pylint: Possible unbalanced tuple unpacking with sequence defined at line 15: left side has 3 label(s), right side has 2 value(s)

print(resp1, resp2, resp3)
print(ImplementedClass().get_hello())


def three_returns() -> Tuple[str, str, str]:
    return "one", "two"


print(three_returns()) # Error escaped


def something(data: str) -> str:
    print(type(data), data)
    return 1 # Supposed to return str, returning int, not caught


value: str = something(2) # Expected str but int returned
print(value.upper()) # Pylint: Instance of 'int' has no 'upper' member

As mentioned in the code block, when incorrect object is returned, pylint or python will never throw any error. It is only when it is explicitly mapped or any class methods are invoked like str.upper() that's when the error is thrown. This would lead to testing all the paths thoroughly else, it can be sure that code block would work.

Is it how it is and we have live with it or there any better ways to handle it like what we get compile time errors in C++, Java?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜