Skip to content

UnboundLocalError: local variable 'hist' referenced before assignment #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jiedxu opened this issue Apr 10, 2022 · 0 comments
Open
Labels
bug Something isn't working

Comments

@jiedxu
Copy link
Collaborator

jiedxu commented Apr 10, 2022

    def record(word, path=_path):
        """Append queried word in a yaml file in lowercase format.
    
        If the path is directed to yaml file, there will be three
        situations. When there is no such file, we create a new one
        according to the specified file name and write the word. When the
        file does exist, we read its content first.
    
        Args:
            word (str): to be appended at the end of the yaml file.
            path (str): to the yaml file.
    
        Raises:
            ValueError: when the path is not to a yaml file.
        """
        if not path.endswith(".yaml"):
            raise ValueError(f"{path} is not a path to yaml file.")
    
        if not pathlib.Path(path).is_file():
            with open(path, "w") as f:
                yaml.safe_dump([word], f)
        else:
            with open(path, "r") as f:
                try:
                    hist = yaml.safe_load(f)
                except yaml.YAMLError as exc:
                    logger.exception(exc)
    
>           if hist is None:
E           UnboundLocalError: local variable 'hist' referenced before assignment

src/cmdict/history.py:41: UnboundLocalError
@jiedxu jiedxu added the bug Something isn't working label Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant