Skip to content

Conversation

@kajarenc
Copy link
Collaborator

@kajarenc kajarenc commented Oct 28, 2022

📚 Context

Please describe the project or issue background here
We implement AttrDict as Mapping instead of inheriting directly from UserDict to prevent unwanted item or attribute assignments for instances of AttrDict. Also, the goal is to be more specific about the types and protocols and keep it consistent with the Secret object itself.

  • What kind of change does this PR introduce?

    • Bugfix
    • Feature
    • Refactoring
    • Other, please describe:

🧠 Description of Changes

  • Add bullet points summarizing your changes here

    • This is a breaking API change
    • This is a visible (user-facing) change

Revised:

Insert screenshot of your updated UI/code here

Current:

Insert screenshot of existing UI/code here

🧪 Testing Done

  • Screenshots included
  • Added/Updated unit tests
  • Added/Updated e2e tests

🌐 References

Does this depend on other work, documents, or tickets?


Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@kajarenc kajarenc changed the title [WIP] Add __setattr__ and __setitem__ Reworke AttrDict for st.secrets to explicitly disallow item and attribute assignment Oct 31, 2022
@kajarenc kajarenc marked this pull request as ready for review October 31, 2022 16:17
except KeyError:
raise AttributeError(_missing_attr_error_message(attr_name))

def __setitem__(self, key, value):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I don't think we need to overwrite __setitem__ and __setattr__ since it's not required by mapping and it will return the same error anyways. Or did you do it here to adapt the error message?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, I did that to keep exceptions similar to high-level Secrets object, to not show internal machinery (expose AttrDict) to the final user.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh, ok that makes sense. But maybe one aspect to think about: if we add those methods, typing checks will think that they actually exist. So there might be a slight benefit of not adding those, to allow type/linters to directly show it as an error.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, my checks show that with or without __setattr__ or __setitem__ neither PyCharm nor mypy does complain about an attempt to assign to attribute or item. Only pyright complains about __setitem__ missing during item assignment.

But without having methods above, user could write something like this

x = st.secrets.servers.alpha
x.some_ip = "7.7.7.7"

st.write(x.some_ip)

Which will not work in case __setattrs__ explicitly raise an exception.

Anyway, I explicitly added NoReturn return type to the methods https://peps.python.org/pep-0484/#the-noreturn-type to explicitly indicate that they should never return successfully. Let's hope some linters in the future will highlight warnings for this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, sounds good :)

Copy link
Collaborator

@lukasmasuch lukasmasuch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@kajarenc kajarenc merged commit e1ef414 into streamlit:develop Nov 4, 2022
tconkling added a commit to tconkling/streamlit that referenced this pull request Nov 8, 2022
* develop:
  Add mypy fixes (streamlit#5662)
  Don't render stale st.snow and st.ballons elements (streamlit#5401)
  Fix unevaluated dataframe exception formatting (streamlit#5657)
  Update aboutDialog to check SessionInfo.isSet() before referring the instance (streamlit#5518)
  Replace assertEquals->assertEqual to silence deprecation warnings (streamlit#5648)
  Reworke AttrDict for st.secrets to explicitly disallow item and attribute assignment (streamlit#5621)
  Fix pydoc command for streamlit package (streamlit#5535)
  Temporarily have /st-allowed-message-origins double as a healthcheck (streamlit#5642)
  Fix Plotly Charts when exiting out of fullscreen (streamlit#5645)
  Flaky test e2e fixes (streamlit#5610)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

streamlit.runtime.secrets.AttrDict no longer returns True for isinstance() against dict in streamlit 1.14.0

2 participants