fabriziosalmi / UglyFeed

Retrieve, aggregate, filter, evaluate, rewrite and serve RSS feeds using Large Language Models for fun, research and learning purposes
GNU Affero General Public License v3.0
123 stars 4 forks source link

pylint improvements #10

Closed fabriziosalmi closed 5 months ago

fabriziosalmi commented 5 months ago

Working on pylint scores improvements :)


fabriziosalmi commented 5 months ago

6 or more obtained atm, here next challenges with examples on remaining pylint warnings across the project:

Each issue type is explained with examples and solutions:

  1. Missing Timeout Argument for requests.get:

    • Issue: Using requests.get without a timeout can cause your program to hang indefinitely.
    • Solution: Always specify a timeout.
    • Example:
      import requests
      response = requests.get('http://example.com', timeout=10)
  2. Disallowed Names:

    • Issue: Using names like "bar" which are disallowed by your naming convention.
    • Solution: Use more descriptive variable names.
    • Example:
      foo = 42  # Bad
      user_age = 42  # Good
  3. Broad Exception Catch:

    • Issue: Catching too general exceptions like Exception.
    • Solution: Catch specific exceptions.
    • Example:
      try:
       # Code that might throw an exception
      except ValueError:
       # Handle ValueError
      except TypeError:
       # Handle TypeError
  4. Line Too Long:

    • Issue: Lines exceeding the maximum allowed length (usually 100 characters).
    • Solution: Break long lines into shorter ones.
    • Example:

      # Bad
      print("This is a very long line that exceeds the maximum allowed length and should be broken down.")
      
      # Good
      print("This is a very long line that exceeds the maximum allowed length "
         "and should be broken down.")
  5. Consider Using .items() for Dictionary Iteration:

    • Issue: Iterating over dictionary keys and then accessing the values inside the loop.
    • Solution: Use .items() to iterate over both keys and values.
    • Example:

      # Bad
      for key in my_dict:
       value = my_dict[key]
      
      # Good
      for key, value in my_dict.items():
       pass
  6. Unused Variables and Imports:

    • Issue: Declaring variables and importing modules that are never used.
    • Solution: Remove unused variables and imports.
    • Example:

      # Bad
      import os
      foo = 42
      
      # Good
      # Remove unused 'import os' and 'foo' if they are not used elsewhere.
  7. No Member Error:

    • Issue: Using a method or attribute that doesn't exist in the module.
    • Solution: Ensure that you are using the correct attributes/methods and that the module is correctly installed and imported.
    • Example:
      import textstat
      score = textstat.flesch_reading_ease("This is a test.")  # Ensure 'flesch_reading_ease' exists
  8. Redefining Outer Scope Variables:

    • Issue: Redefining a variable name that was defined in an outer scope.
    • Solution: Use unique variable names to avoid shadowing.
    • Example:

      # Bad
      config = {'key': 'value'}
      def function():
       config = {'other_key': 'other_value'}
      
      # Good
      config = {'key': 'value'}
      def function():
       local_config = {'other_key': 'other_value'}
  9. Missing Function or Module Docstrings:

    • Issue: Functions or modules without documentation.
    • Solution: Add docstrings to describe the purpose and usage of functions and modules.
    • Example:
      def add(a, b):
       """Add two numbers and return the result."""
       return a + b
  10. Logging Format Issues:

    • Issue: Using string interpolation directly in logging functions.
    • Solution: Use lazy formatting with %.
    • Example:

      import logging
      logger = logging.getLogger(__name__)
      
      # Bad
      logger.info("User %s logged in", user.name)
      
      # Good
      logger.info("User %s logged in", user.name)
  11. Unspecified Encoding in File Operations:

    • Issue: Opening files without specifying the encoding.
    • Solution: Always specify the encoding.
    • Example:

      # Bad
      with open('file.txt', 'r') as f:
        content = f.read()
      
      # Good
      with open('file.txt', 'r', encoding='utf-8') as f:
        content = f.read()
  12. Too Many Local Variables or Statements:

    • Issue: Functions with too many local variables or statements.
    • Solution: Refactor the function into smaller, more manageable pieces.
    • Example:

      # Bad
      def process_data(data):
        var1 = ...
        var2 = ...
        ...
        # many lines of code
      
      # Good
      def process_data(data):
        processed_data = step_one(data)
        results = step_two(processed_data)
        return results

Next Steps

fabriziosalmi commented 5 months ago

added GitHub action to get pylint feedback at push events