Skip to content

Instantly share code, notes, and snippets.

@dnutiu
Created June 26, 2023 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnutiu/b0df79da27e0709e5adaf3d173bd58e5 to your computer and use it in GitHub Desktop.
Save dnutiu/b0df79da27e0709e5adaf3d173bd58e5 to your computer and use it in GitHub Desktop.

The function:

@lru_cache()
def movie_repository(settings: Settings = Depends(settings_instance)):
    """
    Movie repository instance to be used as a Fast API dependency.
    """
    return MongoMovieRepository(
        connection_string=settings.mongo_connection_string,
        database=settings.mongo_database_name,
    )

Will cause the following exception:

TypeError: unhashable type: 'Settings'

To fix the exception, please override hash function from Settings as so:

class Settings(BaseSettings):
    [...]
    def __hash__(self) -> int:
        return 1

The course bypasses the exception by using a closure function but that doesn't work as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment