• Detect naming typos (AttributeError) in function names

    From c.buhtz@posteo.jp@21:1/5 to All on Mon Nov 6 12:47:57 2023
    Hello,

    I would like to know how to detect (e.g. via a linter) typos in function
    names imported from another module.

    Let's assume this given Python code snippet.

    import foo
    foo.baR()

    The package "foo" do contain a function named "bar()" (all lower case
    letters). The function "baR()" does not exist in "foo". This cause an AttributeError when run with a Python interpreter.

    The described error is not detected in my IDE (Emacs with eglot, pylsp
    and flake8) and not by flake8 on the shell. Because the involved tools
    do not look inside the "foo" package if "baR()" really exist.

    Can I fix this somehow?

    I am aware that this would get detected by a unit test. That is the way
    I do prefer in most cases. But sometimes not all code segments are
    covered by tests. I'm more interested in using a linter or something
    else for that.

    Kind
    Christian

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dieter Maurer@21:1/5 to c.buhtz@posteo.jp on Mon Nov 6 19:11:41 2023
    c.buhtz@posteo.jp wrote at 2023-11-6 12:47 +0000:
    I would like to know how to detect (e.g. via a linter) typos in function >names imported from another module.

    One option is a test suite (--> Python's "unittest" package)
    with a sufficiently high coverage (near 100 %).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From George Fischhof@21:1/5 to All on Mon Nov 6 20:37:32 2023
    Dieter Maurer via Python-list <python-list@python.org> ezt írta (időpont: 2023. nov. 6., H, 19:13):

    c.buhtz@posteo.jp wrote at 2023-11-6 12:47 +0000:
    I would like to know how to detect (e.g. via a linter) typos in function >names imported from another module.

    One option is a test suite (--> Python's "unittest" package)
    with a sufficiently high coverage (near 100 %).
    --
    https://mail.python.org/mailman/listinfo/python-list



    Hi

    PyCharm IDE warns you, also vulture
    https://pypi.org/project/vulture/
    finds dead code (not called / not used because of typo), and if you compile
    the code:
    https://docs.python.org/3/library/py_compile.html
    it will generate syntax error if non-existent function is called.
    linters can perhaps warn you (never had typos, because I use PyCharm)
    need to check the linters' doc

    BR,
    George

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From c.buhtz@posteo.jp@21:1/5 to All on Tue Nov 7 07:48:29 2023
    Hello Dieter,

    thanks for your reply.

    Am 06.11.2023 19:11 schrieb Dieter Maurer:
    One option is a test suite (--> Python's "unittest" package)
    with a sufficiently high coverage (near 100 %).

    Yes, that is the primary goal. But it is far away in the related
    project.

    I got a hint that "pylint" is able to detect problems like this.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Christian Buhtz via Python-list on Tue Nov 7 07:35:18 2023
    On 11/7/2023 2:48 AM, Christian Buhtz via Python-list wrote:
    Hello Dieter,

    thanks for your reply.

    Am 06.11.2023 19:11 schrieb Dieter Maurer:
    One option is a test suite (--> Python's "unittest" package)
    with a sufficiently high coverage (near 100 %).

    Yes, that is the primary goal. But it is far away in the related project.

    I got a hint that "pylint" is able to detect problems like this.

    mypy can detect typos in names by noticing that they haven't been
    declared. For example, if you have a class NewClass(BaseClass), and
    BaseClass has a method findme(), but you call it as findMe(), mypy will
    tell you findMe does not exist in BaseClass. It can be annoying to get
    the options set right so you don't get too many undesired hits, but it's certainly doable. mypy can be slow, depending on your code.

    You could also simply run py_compile, which will try to compile the
    code. It will stop at the first error it finds.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)