Mailing List Archive

Detect naming typos (AttributeError) in function names
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
--
https://mail.python.org/mailman/listinfo/python-list
Re: Detect naming typos (AttributeError) in function names [ In reply to ]
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
Re: Detect naming typos (AttributeError) in function names [ In reply to ]
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
--
https://mail.python.org/mailman/listinfo/python-list
Re: Detect naming typos (AttributeError) in function names [ In reply to ]
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.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Detect naming typos (AttributeError) in function names [ In reply to ]
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.

--
https://mail.python.org/mailman/listinfo/python-list