Quantcast
Channel: Getting rid of PyCharm warning for Python static/class property - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Answer by jrbergen for Getting rid of PyCharm warning for Python static/class property

$
0
0

You may want to make a separate predefined subclass of Result for a default 'Success' result.

(I misinterpreted your question at first; in case one finds this question and is just looking for a way to disable PyCharm inspections locally or globally; scroll down).

E.g:

class Result(object):    def __init__(self, success: bool,                       details: str):      self.success = success      self.details = detailsclass ResultSuccess(Result):    def __init__(self, success=True,                       details=''):        super(Result, self).__init__(success,                                     details)foo = some_class.first_thing()if not foo:   return Result(False, "first_thing failed")bar = some_class.second_thing()if bar:   return ResultSuccess()else:   return Result(False, "second_thing failed")

Note that for this to work in Python 2.7, you need to have the baseclass explicitly inheriting from object, to enable new-style class functionality and allow usage of super().

In Python 3, this is not needed, and super(Result, self).__init__(success=True, details='') can be replaced by just super().__init__(success=True, details='')

Preventing instantiation overhead

At risk of addressing too broad a scope, the potential issue of instantiating a new object every time is discussed shortly, as a reply to OP's comment.

Before you go this route, consider if it matters that the ResultSuccess object is instantiated on every use. Unless you instantiate it thousands to hundreds of thousands of times in quick succession, I think it is unlikely to meaningfully impact performance for most use cases (haven't tested it though). Besides, the object instances will be garbage collected if no longer used/referenced and shouldn't use memory after the scope where they are used is left.

If you want to do it anyway, you could use either a separate file i.e. module where you initialize a ResultSuccess; this also means you won't have to subclass:

# static_succesresult.pyclass Result(object):   def __init__(self, success: bool,                      details: str):      self.success = success      self.details = detailsResultSuccess = Result(True, details='')# elsewhere.pyfrom static_successresult import ResultSuccess#logic which uses ResultSuccess

This should make it global. Alternatively you could make ResultSucces a singleton in various ways (I like the metaclass way of doing it if a module wouldn't suffice). Note however that this pattern is controversial. In my opinion it should be used with care, for reasons which are out of scope here. I think it's fine for a small class like this which shouldn't change state, but there are likely better ways to approach this. (You could also use the built-in Enum for example, although it also makes use of singletons.).

  

Disabling PyCharm inspections

You could disable particular inspections globally in the settings. Often one doesn't want to do this globally however.

This can also be done locally by placing:

# noinspection {particular_inspection_type}

... above a function or statement for which you would like to disable it. In your case, this would be:

# noinspection PyUnresolvedReferencesdef function_example():    return seemingly_unresolved_variable

or

# noinspection PyUnresolvedReferencesstatement_example = seemingly_unresolved_variable

Fortunately, PyCharm can do this for you automatically, so you don't have to remember the names for all the inspection types.

Disable inspection highlighting on a per-file basis:

It is possible to disable the highlighting for inspections on a per-file basis as well, although not for a particular type of inspection; 'syntax', 'All problems', and 'None' are the available options. You can click on the warning symbol in the top-hand corner and select the desired option from the drop-down menu:

context menu in the right-hand corner allows one to choose degree of problem highlighting for the current file

Disabling inspections per statement or function:

(1) PyCharm underlines the code at issue having an unresolved reference:PyCharm underlines the code at issue having an unresolved reference

(2) Open the context menu (default=alt+enter):Open the context menu (default=alt+enter)

(3) Select disable inspection (for the desired context) in second context menu. In this case only 'suppress for statement' can be chosen as we are not in the context of a function:

Select disable inspection (for the desired context) in second context menu

(4) A line is added which will have PyCharm ignore the inspection for the selected scope:

A line is added which will have PyCharm ignore the inspection for the selected scope.

For more info, see 'Disabling and enabling inspections' on Jetbrain's website.

Below follows a textual description for suppressing warnings for functions/statements in case one is prevented from seeing the images:

When you bring up the context menu for an underlined statement (should be alt+enter by default, having the cursor placed at the code at issue), a suggestion to correct the problem pops up. If you pick this, a new dropdown menu appears which asks you to correct the problem. If you then use the right arrow or click on the right arrow for the suggested correction, a new dropdown menu appears with options like "suppress for statement" or "suppress for function".

Choose the one applicable to your case to automatically add a line indicating that the linter should ignore a particular inspection for the selected scope.

In your case, this would be:

# noinspection PyUnresolvedReferences.


Viewing all articles
Browse latest Browse all 3

Trending Articles





<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>