Search code examples
python-sphinxrestructuredtextdocstring

Sphinx is throwing some indentation errors, and I cannot figure out how to fix them


These are the errors I am getting from Sphinx:

  1. Unexpected indentation: [{ "resource": "/c:/Users/User/Documents/GitHub/MIMIC/mimic/model_simulate/base_model.py", "owner": "generated_diagnostic_collection_name#5", "severity": 8, "message": "Unexpected indentation.", "source": "sphinx", "startLineNumber": 46, "startColumn": 1, "endLineNumber": 47, "endColumn": 1 }]
  2. Definition list ends without a blank line; unexpected unindent: [{ "resource": "/c:/Users/User/Documents/GitHub/MIMIC/mimic/model_simulate/base_model.py", "owner": "generated_diagnostic_collection_name#5", "severity": 4, "message": "Definition list ends without a blank line; unexpected unindent.", "source": "sphinx", "startLineNumber": 44, "startColumn": 1, "endLineNumber": 45, "endColumn": 1 }]
  3. Block quote ends without a blank line; unexpected unindent: [{ "resource": "/c:/Users/User/Documents/GitHub/MIMIC/mimic/model_simulate/base_model.py", "owner": "generated_diagnostic_collection_name#5", "severity": 4, "message": "Block quote ends without a blank line; unexpected unindent.", "source": "sphinx", "startLineNumber": 47, "startColumn": 1, "endLineNumber": 48, "endColumn": 1 }]

and this is the docstring:

    """
    Abstract base class for creating and managing simulation models.

    This class serves as a foundation for any type of model that requires
    managing data, parameters, and basic I/O operations. It defines a
    common interface for parameter handling, data simulation, and data
    persistence.

    Attributes:
        data (np.ndarray | None): Holds the output data generated by the model's simulation.
            This could be None if the model has not yet produced any data.
        model (object | None): A generic placeholder for the specific simulation model instance.
            This attribute should be overridden in subclasses with an actual
            model representation.
        parameters (dict | None): A dictionary containing the parameters that control the model's
            behavior. Parameters should be defined in subclasses or set through
            the provided methods.

    Abstract Methods:
        set_parameters(self): Should be implemented by subclasses to define how model parameters
            are set or updated.
        simulate(self): Should be implemented by subclasses to define the model's simulation process
            based on the set parameters.

    Methods:
        check_params(self, params, sim_type): Checks provided parameters against required ones for a
            given simulation type, applying default values if necessary.
        read_parameters(self, filepath): Reads model parameters from a specified JSON file and updates
            the model's parameters accordingly.
        save_parameters(self, filepath, parameters=None): Saves the model's current parameters to a
            JSON file. Optionally, a specific set of parameters can be provided to save instead.
        print_parameters(self, precision=2): Prints the current set of model parameters to the console,
            formatting numpy arrays with specified precision.
        save_data(self, filename, data=None): Saves the model's generated data to a CSV file. Optionally,
            specific data can be provided to save instead.
        load_data(self, filename): Loads data from a specified CSV file into the model's `data` attribute.
        _custom_array_to_string(self, array, precision=2): Converts a numpy array to a string representation
            with specified precision.
        update_attributes(self): Updates class attributes based on the current parameters dictionary.
    """

The docstring starts at line 10.

Help!


Solution

  • How to debug:

    • Copy paste the offending docstring content into a text editor.
    • Save and translate with rst2html. (Translate with Sphinx, if the source contains Sphinx-extensions to standard reStructuredText.)

    The error messages point to the relevant lines in your source document.

    Fix indentation of line 35:

         save_data(self, filename, data=None): Saves the model's generated data to a CSV file. Optionally,
             specific data can be provided to save instead.
         load_data(self, filename): Loads data from a specified CSV file into the model's `data` attribute.
    -    _custom_array_to_string(self, array, precision=2): Converts a numpy array to a string representation
    +        _custom_array_to_string(self, array, precision=2): Converts a numpy array to a string representation
             with specified precision.
         update_attributes(self): Updates class attributes based on the current parameters dictionary.
    

    Make the last line a definition list item:

         load_data(self, filename): Loads data from a specified CSV file into the model's `data` attribute.
         _custom_array_to_string(self, array, precision=2): Converts a numpy array to a string representation
             with specified precision.
    -    update_attributes(self): Updates class attributes based on the current parameters dictionary.
    +    update_attributes(self):
    +        Updates class attributes based on the current parameters dictionary.
    

    You may also want to fix all other definition list entries with a newline after the definition term and properly indented description (cf. definition lists).