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

Add type annotations #464

Merged
merged 29 commits into from
Jul 25, 2023
Merged

Add type annotations #464

merged 29 commits into from
Jul 25, 2023

Conversation

shuttle1987
Copy link
Contributor

These changes add in type annotations to the code.

Copy link
Member

@adrien-berchet adrien-berchet left a comment

Choose a reason for hiding this comment

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

Very nice, thanks for this PR!!!

geoalchemy2/elements.py Outdated Show resolved Hide resolved
geoalchemy2/elements.py Show resolved Hide resolved
@adrien-berchet
Copy link
Member

Another thing: it could be nice to add some tests with mypy to check the annotations are correct.

@adrien-berchet adrien-berchet linked an issue Jul 7, 2023 that may be closed by this pull request
@adrien-berchet
Copy link
Member

Also, I think we have to add a py.typed file, right? https://peps.python.org/pep-0561/#packaging-type-information

@shuttle1987
Copy link
Contributor Author

As far as I understand py.typed is for when you are distributing type stub files. Because we aren't supporting Python 2 I don't know if we need this?

@shuttle1987
Copy link
Contributor Author

I'm not so familiar with the CI pipeline being used for this project but I'm curious as to why the linting step in tox fails only for python==3.8?

@adrien-berchet
Copy link
Member

As far as I understand py.typed is for when you are distributing type stub files. Because we aren't supporting Python 2 I don't know if we need this?

I'm not sure either, I never setup type hints for a library that will be used in other codes. Did you try to run Mypy on another code that includes GeoAlchemy2 objects?

I'm not so familiar with the CI pipeline being used for this project but I'm curious as to why the linting step in tox fails only for python==3.8?

It's just because the lint tox job is run only in the Py38 CI job.

@shuttle1987
Copy link
Contributor Author

That makes sense about the single job failing if that's the only place the liniting is run. I've pushed a couple of commits to attempt to deal with the linter complaints

@adrien-berchet
Copy link
Member

Sorry I should have tell you that before but you can run tox r -e format locally to fix most of the simple linting issues.

@adrien-berchet
Copy link
Member

Now it's good, thanks :⁠-⁠)
Do you want to add a few tests for these annotations? Or I can do it later if you prefer. I never tried it but I think we can use this pytest plugin: https://pypi.org/project/pytest-mypy-testing/ . But it can be something else if you know a better solution.

@shuttle1987
Copy link
Contributor Author

I'm not familiar with that package, in other projects I've added a mypy check as part of the CI run.

One other question I had was the types found in geoalchemy2/comparator.py, do we want to annotate return types there? I wasn't sure exactly what those types were supposed to be so I didn't annotate them in this PR.

geoalchemy2/elements.py Outdated Show resolved Hide resolved
@adrien-berchet
Copy link
Member

For comparators I'm not sure. We should check the type annotations in SQLAlchemy and do the same.

@adrien-berchet
Copy link
Member

For comparators I'm not sure. We should check the type annotations in SQLAlchemy and do the same.

It seems that the comparators can get a WKT str, a WKTElement, or a WKBElement and they return a sqlalchemy.sql.expression.BinaryExpression.

@shuttle1987
Copy link
Contributor Author

I'm not so sure how best to annotate the types of the Comparator class, are these changes worth merging as is? Or is anyone able to push a commit with a fix?

@adrien-berchet
Copy link
Member

adrien-berchet commented Jul 23, 2023

As far as I can see, the comparators should actually return sqlalchemy.sql.elements.ColumnElement. And their inputs should be Union[str, WKTElement] (and maybe also WKBElement but I'm not sure?)
Also, could you try to add tests using pytest-mypy please? So we are (almost) sure that annotations are correct.

@adrien-berchet
Copy link
Member

Looks great! Now I think we just have to fix mypy tests, do you need help for it?

@shuttle1987
Copy link
Contributor Author

There was some missing type stub files in the last run, I've attempted to install those in 98d592e

I think there's a few things that will fail however, but I'd like to see what those are from the CI runner. I suspect that we will need to add to the tox configuration some mypy options including the following:

# MYPY
[tool.mypy]
ignore_missing_imports = true

@adrien-berchet
Copy link
Member

There was some missing type stub files in the last run, I've attempted to install those in 98d592e

Nice!

I think there's a few things that will fail however, but I'd like to see what those are from the CI runner. I suspect that we will need to add to the tox configuration some mypy options including the following:

# MYPY
[tool.mypy]
ignore_missing_imports = true

Yeah indeed we will probably have to add some custom config for mypy.

@shuttle1987
Copy link
Contributor Author

Looks like my idea of using mypy --install-types --non-interactive isn't going to work nicely inside a CI run since there's no cache.

@shuttle1987
Copy link
Contributor Author

Looks like we encounter this issue: sqlalchemy/sqlalchemy2-stubs#242

@adrien-berchet
Copy link
Member

Looks like we encounter this issue: sqlalchemy/sqlalchemy2-stubs#242

Since this issue is supposed to be fixed in SQLAlchemy>=2 I suggest we run Mypy only for the *-sqlalatest jobs. WDYT?

@shuttle1987
Copy link
Contributor Author

shuttle1987 commented Jul 24, 2023

If you could change the mypy test run to only run vs SQLAlchemy >= 2 that would be great.

@adrien-berchet
Copy link
Member

In the end I could make it work for both sqlalchemy==1.4.* and sqlalchemy>=2.
I fixed several things to make Mypy happy and I added some tests for comparators to be sure. Does it look good to you?

Copy link
Contributor Author

@shuttle1987 shuttle1987 left a comment

Choose a reason for hiding this comment

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

Overall this looks good to me.

geoalchemy2/comparator.py Outdated Show resolved Hide resolved
geoalchemy2/comparator.py Outdated Show resolved Hide resolved
@adrien-berchet adrien-berchet merged commit fd77ba4 into geoalchemy:master Jul 25, 2023
@adrien-berchet
Copy link
Member

Thank you very much for this work @shuttle1987 !
I think I will push a new release soon to make this available.

@adrien-berchet
Copy link
Member

@shuttle1987 I just released the 0.14.1 version, so now you can use Mypy with GeoAlchemy 2 🎉

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.

Add type annotations
2 participants