#πŸ”’ Affirm to checker that method will return value in use case

13 messages Β· Page 1 of 1 (latest)

hard dust
#

I'm positive this has been answered, but I'm not able to find the response, probably because I'm not searching the right terms.

I have this code:

object_session(self).scalars(

object_session(self) can returns Session | None, but in this use case, I know it will return a Session. How can I make the checker (ruff) happy with the line? I know I could do something like

session = object_session(self)
if not session:
  raise Exception()
session.scalars

But that's wordy, specially as if there's no session, there would have been a problem upstream anyway.

analog crestBOT
#

Hey @hard dust!

Please edit your message to use a code block

Add a py after the three backticks.

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
analog crestBOT
#

@hard dust

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

silk nacelle
hard dust
#

Ok, so safety checks, same as my idea of checking if None

#

Which is the "right" way to do it anyway

mossy ermine
hard dust
#

Well, in this case, object_session is coming from SqlAlchemy, so I can't force that

#

I know it will return a session, as the method I'm writing requires there to be a value from the database, so a session would have to be attached anyway

#

And if something goes wrong with the session, I'd want an exception

copper lake
#

"as the method I'm writing requires there to be a value from the database" - suppose it's missing anyway; what would have happened before the code gets to this point?

analog crestBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.