Best practices on Session State variables - Pydantic type highlight and auto-complete


Suppose I have created some variables in session_state in the module.
Suppose also that I import the utils module in another file.

What are the best practices for using session_state variables created in the utils module in the main file so that Pydantic on my IDE (Vs Code) automatically highlights methods for the variable type?
I wonder if there is any way to type annoted a session_state variable across files whithout the need to create a tyoe annotated local variable?

From what I have explored so far, I have found that Pydantic identifies a variable accessed in the session_state as a function regardless of the actual variable type.

The only way around this behaviour I have found was to create type annotated local variables, but is this the best practice? I though that the session_state was created also to make this thing not happen.

Examples below.

Steps to reproduce

Code snippet:


def foo():
  # whithin this file, Pydantic will now auto-complete and higlight list methods in the lines below
  st.session_state.var_a: list = [0, 1, 2]
  # when typing the below command the list method append will be highlighted


from utils import foo


# now if I need to access the session_state variable created in the utils module Pydantic will not highlight the list method

# the only way I found to make the Pydantic highlitgh work, is to assign the session_state variable to a type annotated local variable, as below
var_a: list = st.session_state.var_a

# now whithin this file the Pydantic highlighting will work for the local variable

Debug info

  • Streamlit version: 1.22
  • Python version: 3.11
  • PipEnv
1 Like

In order to make Pydantic highlight the methods for session_state variables across files, you can use type annotations. However, instead of creating type annotated local variables, you can directly annotate the session_state variable itself in the main file.

Hereโ€™s an example of how you can do this:

import streamlit as st

def foo():
    st.session_state.var_a: list = [0, 1, 2]

from utils import foo
from streamlit import session_state

# Annotate the session_state variable in the main file
session_state.var_a: list


# Now Pydantic will highlight the list methods for the session_state variable

By explicitly annotating the session_state.var_a variable in the main file, you can make Pydantic recognize its type and provide auto-completion and highlighting for list methods.

This way, you donโ€™t need to create a separate type annotated local variable, and you can directly work with the session_state variable while still getting the Pydantic highlighting and auto-completion.

Hi @ShraavaniTople, thank you for helping me!

I have tried this approach as you said, but in VS Code even with the type annotated variable in the main file, Pydantic doesnโ€™t recognize it as a variable, it indicates that the call to the session_state api is a function, not a variable so the auto-completion doesnโ€™t work.

Have you tested it in Vs Code?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.