python - How to ignore pyright linter hints - Stack Overflow

admin2025-05-01  1

In Python, I'd like to "disable" (or render unusable) an inherited class method in my queue data structure. I do this as follows:

class LLQueue(LinkedList):
    def add_to_head(self, val):
        raise NotImplementedError("Cannot add to head of a queue - use add_to_tail instead")

This works as intended, but the pyright linter provides a hint diagnostic: "val" is not accessed. I was hoping to suppress the hint diagnostic with a # type: ignore in-line comment (or a similar comment), but this does not seem to work.

In Python, I'd like to "disable" (or render unusable) an inherited class method in my queue data structure. I do this as follows:

class LLQueue(LinkedList):
    def add_to_head(self, val):
        raise NotImplementedError("Cannot add to head of a queue - use add_to_tail instead")

This works as intended, but the pyright linter provides a hint diagnostic: "val" is not accessed. I was hoping to suppress the hint diagnostic with a # type: ignore in-line comment (or a similar comment), but this does not seem to work.

Share Improve this question asked Jan 2 at 20:37 Jeff FavretJeff Favret 554 bronze badges 4
  • This thing probably shouldn't inherit from LinkedList if it's not going to support the operations LinkedList supports. Consider using composition instead of inheritance. – user2357112 Commented Jan 2 at 20:42
  • Give the parameter a dunder name to suppress the warning: def add_to_head(self, __val__): – Barmar Commented Jan 2 at 21:22
  • @Barmar This worked, thanks! Can you please explain why it works, and perhaps post it as a solution so that I can accept it? Thanks again! – Jeff Favret Commented Jan 2 at 23:36
  • @user2357112 I am still learning and had to look up composition versus inheritance. Your suggestion makes a lot of sense. I'll refactor the code and get the added benefit of the linter not yelling at me. Thanks! – Jeff Favret Commented Jan 2 at 23:57
Add a comment  | 

1 Answer 1

Reset to default 0

Start the parameter name with an underscore, e.g.

    def add_to_head(self, _val):

From the documentation:

reportUnusedVariable [boolean or string, optional]: Generate or suppress diagnostics for a variable that is not accessed. The default value for this setting is "none". Variables whose names begin with an underscore are exempt from this check.

转载请注明原文地址:http://anycun.com/QandA/1746097715a91633.html