{"id":9173,"library":"perflint","title":"Perflint: Pylint Extension for Performance Anti-patterns","description":"Perflint is an extension for Pylint that identifies performance anti-patterns in Python code, helping developers optimize their applications by flagging common inefficiencies. It is currently in early beta, version 0.8.1, and may produce false positives. The project is actively maintained with a focus on enhancing code performance through static analysis.","status":"active","version":"0.8.1","language":"en","source_language":"en","source_url":"https://github.com/tonybaloney/perflint","tags":["linter","pylint-extension","performance-optimization","static-analysis","developer-tool"],"install":[{"cmd":"pip install perflint","lang":"bash","label":"Install Perflint"}],"dependencies":[{"reason":"Perflint functions as a plugin for Pylint.","package":"pylint","optional":false}],"imports":[{"note":"Perflint is not imported as a Python module in application code. It's loaded as a plugin for Pylint via the command line or configuration.","wrong":"from perflint import some_check","symbol":"Perflint as Pylint Plugin","correct":"pylint your_module/ --load-plugins=perflint"}],"quickstart":{"code":"import os\n\ndef process_data(items):\n    # W8202: Global name usage in a loop\n    # os.environ is a global lookup; cache it outside the loop for performance.\n    for item in items:\n        value = os.environ.get(item, 'default') # This will trigger W8202\n        print(value)\n\ndef unnecessary_list_cast_example(data):\n    # W8101: Unnecessary use of list() on an already iterable type\n    for x in list(data): # data is already iterable, no need for list()\n        print(x)\n\ndef incorrect_dict_iterator_example(dictionary):\n    # W8102: Incorrect iterator method for dict\n    for _, value in dictionary.items(): # If only values are needed, use .values()\n        print(value)\n\nif __name__ == '__main__':\n    my_items = ['HOME', 'PATH']\n    process_data(my_items)\n    my_tuple = (1, 2, 3)\n    unnecessary_list_cast_example(my_tuple)\n    my_dict = {'a': 1, 'b': 2}\n    incorrect_dict_iterator_example(my_dict)\n","lang":"python","description":"Save the code above as `my_module.py`. Then run Perflint as a Pylint plugin. It will highlight performance anti-patterns like global lookups in loops, unnecessary list casting, and inefficient dictionary iteration."},"warnings":[{"fix":"Always manually verify reported performance anti-patterns before refactoring code based on Perflint warnings.","message":"Perflint is currently an 'early beta' and may generate false positives, requiring careful review of its findings.","severity":"gotcha","affected_versions":"0.1.0 - 0.8.1"},{"fix":"Refactor code to place the entire loop within a single `try` block, or move exception handling outside the loop if possible, to avoid per-iteration overhead.","message":"Using `try...except` blocks inside loops can have significant computational overhead, especially in Python versions prior to 3.11 (R8203).","severity":"breaking","affected_versions":"<=3.10"},{"fix":"Import the specific function or attribute directly before the loop (e.g., `from os.path import exists`) to reduce lookup overhead.","message":"Dotted imports (e.g., `os.path.exists`) inside loops can be inefficient because Python performs multiple lookups in each iteration (W8205).","severity":"gotcha","affected_versions":"All versions"},{"fix":"Cache the global variable's value in a local variable outside the loop, then use the local variable inside the loop.","message":"Accessing global variables within a tight loop can be slower than using local variables (W8202).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure `perflint` is installed in the same virtual environment as Pylint (`pip install perflint`). Also, ensure Pylint is up-to-date.","cause":"Perflint is not installed in the environment where Pylint is being run, or Pylint's version is too old to support plugin loading.","error":"pylint: error: no such option: --load-plugins=perflint"},{"fix":"Remove the `list()` constructor if the iterable type already suffices, e.g., `for item in my_tuple:` instead of `for item in list(my_tuple):`.","cause":"Using `list()` to cast an already iterable object (like a tuple or generator) into a list unnecessarily creates a new list object, which can be inefficient.","error":"W8101: unnecessary-list-cast"},{"fix":"Use `for value in my_dict.values():` if only values are needed, or `for key in my_dict.keys():` if only keys are needed.","cause":"Iterating over `dict.items()` and discarding either the key or the value (e.g., `for _, value in my_dict.items():`) is less efficient than using `dict.keys()` or `dict.values()` directly.","error":"W8102: incorrect-dictionary-iterator"}]}