135201
You will be muted until you read the rules. Read them! @Rules_for_Python A group about the Python programming language. Offtopic things go here: @pythonofftopic Resources to learn python: @pythonres Group for Hy: @hylang Selenium: @SeleniumPython
It would be really nice if you would open an issue on Github (or two) stating all of these bugs you found / think you found.
So when i fix them, you get credited.
Its very easy to do. Just follow the first example in this document
https://docs.github.com/en/issues/tracking-your-work-with-issues/using-issues/creating-an-issue#creating-an-issue-from-a-repository
Generators do not work yet
I though we established that
And if you knew what generator is, you would knew that you can call next() on it to produce a value
vivekvjnk/understanding-python-generators-the-power-of-yield-and-state-management-382faa48e761" rel="nofollow">https://medium.com/@vivekvjnk/understanding-python-generators-the-power-of-yield-and-state-management-382faa48e761
see as you say i found and started using this library, lastly all before thigns works, but it got stuck when i use the di to do real database crud operation, there it say, but still i found the a fun whihc do :
yield session, this function not yeield the session but it return a generator, and thats why i cannot perform any crud functions there, i will try to raise one issue in the repository with all the things,
def get_session() -> Generator[Session, None, None]:
with Session(engine) as session:
yield session
AttributeError: 'generator' object has no attribute 'exec'
as you say to impliment like for my use case i found this is working,
@login_manager.user_loader
def load_user(
user_id: str,
) -> FlaskLoginUser | None:
from flask_di import Depends
user_repository: UserRepository = current_app._resolve_dependency(
Depends(user_repository_provider)
)
obj_domain = user_repository.get_by_id(user_id)
if obj_domain is None:
return None
obj = FlaskLoginUser(obj_domain)
return obj
i wish i could do this type of things like other do.
i am really learning more about di and so on and then come back with you
But it is
Flasks request is in both of these, it should work fine
so in your library if i will use, the ._resolve_dependency() this will call any Depends, if i have
SessionDep = Annotated[...]
will this also work, is htis means, even fun is not part of the current response-request thsi dependency will work always?
and see i listen that this your library will not work with yield, as when i getting explanation of this library in some library with my little code i got to know, as i am not understand very very python concept i got to know like this,
Below Response from ai: was this how right can you check ur code:
There is one important issue here, though.
Your DIFlask implementation does:
value = dep_func(**kwargs)
get_session() literally contains:yield session
get_session()
Generator
Session.di-flask implementation you showed me.
For now, you can
I will implement a public one soon, so it will be just
db = current_app.resolve(get_db)
will i call like this private methods in my application?
Читать полностью…
as thsi is not coming from response fun, so the user_repository:UserRepositoryDep will not resolve right?
see the message of this
i did integrate your library with many codes dependency, now i am not able actually to resolve this as my app just stop at this user_loader in open
i make like this,
def create_app() -> Flask:Читать полностью…
# app = Flask(
# import_name=__name__,
# )
app = DIFlask(
import_name=__name__,
)
login_manager.init_app( # type: ignore
app=app,
)
csrf.init_app( # type: ignore
app=app,
)
app.secret_key = settings.app.secret_key.get_secret_value()
app.register_blueprint(blueprint=auth_bp)
app.register_blueprint(blueprint=general_bp)
I have this big issue, why this not shows how i can use factory pattern here easily, as per the docs,
app = DIFlask(name)
But see, for another case we use,
def create_app() -> Flask:
app = Flask(
import_name=__name__,
)
login_manager.init_app( # type: ignore
app=app,
)
csrf.init_app( # type: ignore
app=app,
)
di_flask = DIFlask()
di_flask.init_app( #This is not working why???
app=app,
)
app.secret_key = settings.app.secret_key.get_secret_value()
app.register_blueprint(blueprint=auth_bp)
app.register_blueprint(blueprint=general_bp)
return app
My app workflow:
register route
│
▼
RegistrationServiceDep
│
▼
get_registration_service()
│
▼
UserRepositoryDep
│
▼
get_sqlmodel_repository()
│
▼
SessionDep
│
▼
get_session()
And nobody is forcing them to use anything
They can use whatever they want, and python venv is the basics of what they need
what establishment, should this yeild sesion will not work in this di-flask, i found it always return generator not the context manager 😢:
ok let me check these thigns btw 😴
Just type cast it into whatever type you want/need
https://docs.python.org/3/library/typing.html#typing.cast
i think if the current_app like things is gives by di_flask it would easy like this same or what as this curretn_app._resolve_dependency is the only problem without any return type like this so it always shows Unknown and shows problem?
Читать полностью…
https://github.com/razorblade23/di-flask/blob/master/src/flask_di/core.py#L86-L114 so you just say to make a more public interface of this methods, this will so simple i can also do this, should i cahnge the library and make a methods and call this private from that, but as the .venv/lib/flask_di, is a library code even i will edit this this not tracked by github, and as a result later this will not work, so no way to use until then, can i change this and then how i can send to github so that you can check that?
Читать полностью…
I did not copy pasted fastapi Depends
So my library is not the same as the one from fastapi
It aims to be, but its far from it.
Raise an issue on Github if you think something is not implementated but should be (like this thing with generators)
Its one step more to becoming developer
Participate in other people projects, learn from them
pls i am newly learnign integrating this library, and i am just trying to use like this, ,
i want to point as this say, the di-flask not performs cleanup, -> what about this, as fastapi docs say after this response done it will clean, here is this also works or not?
And where exactly do i use yield in my *litle library* docs?
I dont see it?
Please stop, if you think i should add something, raise an issue or make a PR like a normal developer.
And nobody is forcing you to use it 🤷♂
Nobody is forcing you to do anything 🤷♂
why you write like this why not
def get_session() -> Generator[Session, None, None]:
with Session(engine) as session:
yield session
This is actually something i may need to add to library
You can use this example as a workaround until i implement a public method for this specific usecase (and others flask may need)
from flask import current_app
from flask_login import LoginManager
from flask_di import Depends
login_manager = LoginManager()
def get_db():
return SessionLocal()
@login_manager.user_loader
def load_user(user_id):
db = current_app._resolve_dependency(Depends(get_db))
return db.get(User, int(user_id))
the problem is @login_manager.user_loader
here i cann't able to pass the Annotated's Depends from your library, as how this will resolve the Dependency?
Читать полностью…
@login_manager.user_loader
def load_user(
user_id: str,
user_repository: UserRepositoryDep,
) -> FlaskLoginUser | None:
obj_entity = user_repository.get_by_id(
user_id=user_id,
)
if obj_entity is None:
return None
return FlaskLoginUser(obj_entity)
What part of
For automatic injection of dependecies we need to wrap Flask class
DIFlask and use that as your flask appfrom flask_di import DIFlask
app = DIFlask(__name__)
i also want to for checking i used this type of code, but the problem is the dependency will how work in this user_loader callback
@login_manager.user_loader # type: ignore
def load_user(
user_id: str,
user_repository: UserRepositoryDep,
) -> FlaskLoginUser | None:
"""
https://flask-login.readthedocs.io/en/latest/#how-it-works
As per the docs it should return the obj or the None.
This will read the user_id from the cookie and convert into the user obj
from the user_obj i will get differnt data to use.
"""
obj_entity = user_repository.get_by_id(
user_id=user_id,
)
if obj_entity is None:
return None
return FlaskLoginUser(obj_entity)
Hello i have started to explore the di and so on and then integrat the di things in my own application and i want to use this perfectly, i used in my DDD structer of code where i am using like this:
for registration new user or login case i need to talk with sqlmodel so it need session, and as the docs satwe will use one engine over my repo and a new session per request so your di-flask library i used here like this i say below i am summarizing the codes form different parts in below:
def get_session() -> Generator[Session, None, None]:
with Session(engine) as session:
yield session
# TODO i need to confirm using this di-flask will ok
SessionDep = Annotated[Session,Depends(get_session)]
def get_sqlmodel_repository(
session: SessionDep,
) -> UserRepository: #This userrepository is a protocol class
return SQLModelUserRepository(
session=session,
)
if config.login_provier_value == "SQLMODEL":
user_repository_provider = get_sqlmodel_repository
UserRepositoryDep = Annotated[
UserRepository,
Depends(
user_repository_provider,
),
]
def get_registration_service(
user_repository: UserRepositoryDep,
) -> RegistrationService:
return RegistrationService( #this has business logics in methods
user_repository=user_repository,
)
# for login_service i will use like this upper UserRepositoryDep
# i will call this below Dep in the routes.py' function
RegistrationServiceDep = Annotated[
RegistrationService,
Depends(
get_registration_service,
),
]
# Below is my routes.py code having this,
@auth_bp.route(
rule="/register",
methods=["GET", "POST"],
)
def register(
register_service: RegistrationServiceDep,
)...
@user_loader which will call .find_user_by_id() method from the UserRepository, but how i will give there the session dep when it will use the Sqlmodel, @login_manager.user_loader
def load_user(user_id):
return User.get(user_id)
As that comes later
Whats the use of uv if you dont know what it does?