Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Outline use of type_annotation_map to fix mypy issues #17902

Merged
merged 3 commits into from
Apr 15, 2024

Conversation

jmchilton
Copy link
Member

Builds on #17897

How to test the changes?

(Select all options that apply)

  • I've included appropriate automated tests.
  • This is a refactoring of components with existing test coverage.
  • Instructions for manual testing are as follows:
    1. [add testing steps and prerequisites here if you didn't write automated tests covering all your changes]

License

  • I agree to license these and all my past contributions to the core galaxy codebase under the MIT license.

@jmchilton jmchilton added the area/database Galaxy's database or data access layer label Apr 4, 2024
@jdavcs jdavcs self-requested a review April 5, 2024 03:52
lib/galaxy/model/__init__.py Outdated Show resolved Hide resolved
lib/galaxy/model/__init__.py Outdated Show resolved Hide resolved
@jmchilton jmchilton marked this pull request as ready for review April 8, 2024 18:38
@github-actions github-actions bot added this to the 24.1 milestone Apr 8, 2024
@jmchilton jmchilton mentioned this pull request Apr 11, 2024
5 tasks
Comment on lines +220 to +221
Optional[STR_TO_STR_DICT]: JSONType,
Optional[TRANSFORM_ACTIONS]: MutableJSONType,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Optional[STR_TO_STR_DICT]: JSONType,
Optional[TRANSFORM_ACTIONS]: MutableJSONType,
STR_TO_STR_DICT: JSONType,
TRANSFORM_ACTIONS: MutableJSONType,

We use Optional in the model definition

Copy link
Member Author

Choose a reason for hiding this comment

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

Well my thinking was the thing in map needed to match the column definition completely. If it doesn't have to match the column definition exactly... I think we don't actually need this map right? I am thinking we just drop this if it isn't actually needed. We would have test failures if this was needed and wrong.

Copy link
Member

Choose a reason for hiding this comment

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

@jmchilton it works as intended; I've reread the docs and tested it to make sure I'm not misreading stuff. Here's how I read it:

STR_TO_STR_DICT = Dict[str, str]  # we define a type alias
...
STR_TO_STR_DICT: JSONType  
# in the statement above (type annotation map), we say that the Python type STR_TO_STR_DICT 
# should be mapped to SQLAlchemy's JSONType, which is a SQLAlchemy type that knows how to 
# generate its DDL. 
# If we substituted JSONType for Integer, the Python code would still expect Dict[str, str],
# but in the db that field would be defined as an integer type.
...
object_store_id_overrides: Mapped[Optional[STR_TO_STR_DICT]]  # that's all we need here!
# the field will be typed as Optional[Dict[str, str]], represented as bytea in the database. 

The Optional type specifier works fine: here's an example that causes a mypy error; removing Optional fixes it - as expected:

def test(job: Job):
    job.object_store_id_overrides['key']
test(Job())

# lib/galaxy/model/__init__.py:11430: error: Value of type "Optional[dict[str, str]]" is not indexable  [index]

Does this make sense?

(There's a conflict to resolve - I'm happy to take care of this.)

Copy link
Member Author

@jmchilton jmchilton Apr 15, 2024

Choose a reason for hiding this comment

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

Maybe this makes sense - I think given that we don't have a typed way to distinguish between what should be mutable or not currently we should drop the dictionary and keep being explicit but I will defer to you until I understand it better.

Update: I mean like we probably have some string to string dictionaries that are mutable and some that are not right? And regardless - we might want to add some or either variety even if we only have one type currently?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, i think so (re. your update). Also, that type annotation map may be useful for other type mappings (SA's docs have some examples here), even if the only benefit in those cases would be more succinct code.

@jdavcs
Copy link
Member

jdavcs commented Apr 11, 2024

Since we've decided to keep HasTable, the second commit (82fd47b) needs to be dropped. I can do that and push to your branch and merge if that's OK.

Also, to make DeclarativeBase work, we need this commit: dcf2d84, but that is part of #17922, which I'll merge today.

Copy link
Member

@jdavcs jdavcs left a comment

Choose a reason for hiding this comment

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

Thanks @jmchilton!

(all: please don't merge before 82fd47b commit is dropped)

@jdavcs jdavcs mentioned this pull request Apr 12, 2024
4 tasks
@@ -1392,7 +1406,7 @@ class Job(Base, JobLike, UsesCreateAndUpdateTime, Dictifiable, Serializable):
params: Mapped[Optional[str]] = mapped_column(TrimmedString(255), index=True)
handler: Mapped[Optional[str]] = mapped_column(TrimmedString(255), index=True)
preferred_object_store_id: Mapped[Optional[str]] = mapped_column(String(255))
object_store_id_overrides: Mapped[Optional[bytes]] = mapped_column(JSONType)
object_store_id_overrides: Mapped[Optional[STR_TO_STR_DICT]] = mapped_column(JSONType)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
object_store_id_overrides: Mapped[Optional[STR_TO_STR_DICT]] = mapped_column(JSONType)
object_store_id_overrides: Mapped[Optional[STR_TO_STR_DICT]]

I think we don't need this: it's derived from the type_annotation_map.
Same for transform below.

@jdavcs jdavcs merged commit 372b8d6 into galaxyproject:dev Apr 15, 2024
51 of 52 checks passed
@nsoranzo nsoranzo deleted the dev_model_newbase branch April 15, 2024 17:42
@galaxyproject galaxyproject deleted a comment from github-actions bot Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/database Galaxy's database or data access layer kind/enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants