Repository: redis/redis-py
File: redis/_parsers/commands.py (Python)

Code before:
        else:
            command_details = self.commands.get(command_name, None)
            if command_details is not None:
                return command_details['first_key_pos'] <= 0

            raise ValueError(f"Command {command_name} not found in commands")

    def get_command_policies(self) -> PolicyRecords:
        """
        Retrieve and process the command policies for all commands and subcommands.

        This method traverses through commands and subcommands, extracting policy details
        from associated data structures and constructing a dictionary of commands with their
        associated policies. It supports nested data structures and handles both main commands
        and their subcommands.

        Returns:
            PolicyRecords: A collection of commands and subcommands associated with their
            respective policies.

        Raises:
            IncorrectPolicyType: If an invalid policy type is encountered during policy extraction.
        """
        command_with_policies = {}

        def extract_policies(data, command_name, module_name):
            """
            Recursively extract policies from nested data structures.
            
            Args:
                data: The data structure to search (can be list, dict, str, bytes, etc.)
                command_name: The command name to associate with found policies
            """
            if isinstance(data, (str, bytes)):
                # Decode bytes to string if needed
                policy = data.decode() if isinstance(data, bytes) else data

                # Check if this is a policy string
                if policy.startswith('request_policy') or policy.startswith('response_policy'):
                    if policy.startswith('request_policy'):
                        policy_type = policy.split(':')[1]

                        try:
                            command_with_policies[module_name][command_name].request_policy = RequestPolicy(policy_type)
                        except ValueError:
                            raise IncorrectPolicyType(f"Incorrect request policy type: {policy_type}")

                    if policy.startswith('response_policy'):
                        policy_type = policy.split(':')[1]

                        try:

Review comment (suggestion):
Since the structure in command_with_policies follows the pattern module_name --> command_name, maybe the order of the arguments in this method should also follow this pattern, for better readability.

Code after:
        else:
            command_details = self.commands.get(command_name, None)
            if command_details is not None:
                return command_details['first_key_pos'] <= 0

            raise ValueError(f"Command {command_name} not found in commands")

    def get_command_policies(self) -> PolicyRecords:
        """
        Retrieve and process the command policies for all commands and subcommands.

        This method traverses through commands and subcommands, extracting policy details
        from associated data structures and constructing a dictionary of commands with their
        associated policies. It supports nested data structures and handles both main commands
        and their subcommands.

        Returns:
            PolicyRecords: A collection of commands and subcommands associated with their
            respective policies.

        Raises:
            IncorrectPolicyType: If an invalid policy type is encountered during policy extraction.
        """
        command_with_policies = {}

        def extract_policies(data, module_name, command_name):
            """
            Recursively extract policies from nested data structures.
            
            Args:
                data: The data structure to search (can be list, dict, str, bytes, etc.)
                command_name: The command name to associate with found policies
            """
            if isinstance(data, (str, bytes)):
                # Decode bytes to string if needed
                policy = str_if_bytes(data.decode())

                # Check if this is a policy string
                if policy.startswith('request_policy') or policy.startswith('response_policy'):
                    if policy.startswith('request_policy'):
                        policy_type = policy.split(':')[1]

                        try:
                            command_with_policies[module_name][command_name].request_policy = RequestPolicy(policy_type)
                        except ValueError:
                            raise IncorrectPolicyType(f"Incorrect request policy type: {policy_type}")

                    if policy.startswith('response_policy'):
                        policy_type = policy.split(':')[1]

                        try: