Closed saltcable closed 1 year ago
Litestar uses polyfactory to generate openapi examples. It seeds the random seed for random to ensure that the examples are deterministic.
You can set a different random seed on faker after importing litestar to override this.
Why does this happen? The reason is that faker no longer supports seeding a per faker random seed but rather requiires a global random seed.
Already closed the issue! I did some tests with other libraries and found that they work well with Faker. Here's an example code snippet using different libraries:
import faker
import starlette
import fastapi
import flask
import sanic
import quart
fake = faker.Faker()
print(fake.md5())
d71a751d94055529478e28f567c50d9c
5da22a60148536ebf46774c249a5f15a
ba1c26b24bb94047a6d18239c73429d8
It seems that other libraries and Faker handle the seeds correctly. However, Litestar is somehow "messing" with the seeds.
The Faker maintainer responded with the following comment:
Given the litestar maintainer comment in the linked issue, what would be your desired solution for faker? I do not see faker ensuring compatibility with each third-party package which might "mess" with the seeding automatically.
Quick question: I attempted to disable the openapi
feature by using openapi_config=None,
but it didn't have the desired effect. Is there any way to disable Litestar's seed modification
, even if I don't intend to use the openapi
functionality?
Already closed the issue! I did some tests with other libraries and found that they work well with Faker. Here's an example code snippet using different libraries:
Well this is a pretty useless exercise. I already explained what you're seeing. The issue is closed because this is not a bug. I can convert it to a discussion instead.
import faker import starlette import fastapi import flask import sanic import quart fake = faker.Faker() print(fake.md5())
The outputs :
d71a751d94055529478e28f567c50d9c
5da22a60148536ebf46774c249a5f15a
ba1c26b24bb94047a6d18239c73429d8
It seems that other libraries and Faker handle the seeds correctly. However, Litestar is somehow "messing" with the seeds.
The Faker maintainer responded with the following comment:
Given the litestar maintainer comment in the linked issue, what would be your desired solution for faker? I do not see faker ensuring compatibility with each third-party package which might "mess" with the seeding automatically.
Quick question: I attempted to disable the
openapi
feature by usingopenapi_config=None,
but it didn't have the desired effect. Is there any way to disableLitestar's seed modification
, even if I don't intend to use theopenapi
functionality?
Not at present. We could consider adding this but it's a very marginal use case, since you can simply set a seed on your own and even abstract this stuff into a fixture.
I need to understand why exactly this is a big issue for you?
As to the other question - if faker supports seed random per faker instance it will prevent this issue. The problem is the use of a global namespace.
Faker is working fine but when Litestar is imported, it's not random anymore. Same result/output every time.
Steps to reproduce
fake = faker.Faker() print(fake.md5())
python lab.py python lab.py python lab.py