litestar-org / litestar

Production-ready, Light, Flexible and Extensible ASGI API framework | Effortlessly Build Performant APIs
https://litestar.dev/
MIT License
5.45k stars 373 forks source link

[Bug] importing "Litestar" somehow makes "Faker" Not Random anymore #1944

Closed saltcable closed 1 year ago

saltcable commented 1 year ago

Faker is working fine but when Litestar is imported, it's not random anymore. Same result/output every time.

Steps to reproduce

  1. code (lab.py)
    
    import faker
    import litestar  # <<=== the problem

fake = faker.Faker() print(fake.md5())

2. run few time

python lab.py python lab.py python lab.py



### Expected behavior (by removing `import litestar`)
_Random output all the time_
`7442b46046a7c2d7ca080483a0d45ad4`
`a87b76986a9afbb9da63dde9ed4d2495`
`6924bc922dd1db07440ec3298c42afb8`

### Actual behavior (same environment)
_Same every time. for all the functions_
`9fc4f6348d3311fa3376be5aa395877a`
`9fc4f6348d3311fa3376be5aa395877a`
`9fc4f6348d3311fa3376be5aa395877a`

<!-- POLAR PLEDGE BADGE START -->
## Funding
* If you would like to see an issue prioritized, make a pledge towards it!
* We receive the pledge once the issue is completed & verified

<a href="https://polar.sh/litestar-org/litestar/issues/1944">
<picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://polar.sh/api/github/litestar-org/litestar/issues/1944/pledge.svg?darkmode=1">
  <img alt="Fund with Polar" src="https://polar.sh/api/github/litestar-org/litestar/issues/1944/pledge.svg">
</picture>
</a>
<!-- POLAR PLEDGE BADGE END -->
saltcable commented 1 year ago

https://github.com/joke2k/faker/issues/1889

Goldziher commented 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.

saltcable commented 1 year ago

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())

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 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?

Goldziher commented 1 year ago

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 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?

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.