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 some typing information to interval-posets #30989

Closed
fchapoton opened this issue Dec 1, 2020 · 29 comments
Closed

add some typing information to interval-posets #30989

fchapoton opened this issue Dec 1, 2020 · 29 comments

Comments

@fchapoton
Copy link
Contributor

just as a little experiment

CC: @tobiasdiez

Component: combinatorics

Author: Frédéric Chapoton

Branch/Commit: 2272bc3

Reviewer: Tobias Diez

Issue created by migration from https://trac.sagemath.org/ticket/30989

@fchapoton fchapoton added this to the sage-9.3 milestone Dec 1, 2020
@fchapoton
Copy link
Contributor Author

New commits:

99c5273add some basic typing information to Tamari interval posets

@fchapoton
Copy link
Contributor Author

Branch: u/chapoton/30989

@fchapoton
Copy link
Contributor Author

Commit: 99c5273

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 1, 2020

Changed commit from 99c5273 to 3d6644a

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 1, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

3d6644afix bad types

@tobiasdiez
Copy link
Contributor

comment:4

In general, this looks good to me (I have admit I didn't went through the code in detail to check whether the added typing information is correct.)

One suggestion for improvement: for lists, tuple and iterators there are special typing annotation that allow to specify also the type of elements in these lists, e.g. https://docs.python.org/3/library/typing.html#typing.Iterator
After from future import annotations, you can simply write list[str] for a list of strings.

Moreover, it would be good to also annotate the method parameters. For example, https://github.com/microsoft/pyright/blob/master/docs/getting-started.md comments

The annotations that provide most value are on function input parameters, instance variables, and return parameters (in that order).

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 1, 2020

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

e1faf42import annotations from future

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 1, 2020

Changed commit from 3d6644a to e1faf42

@fchapoton
Copy link
Contributor Author

comment:6

Thanks for the hint.

I think it would be slightly premature to add more typing here, without thinking in general about the relationship with the Parent/Element framework.

@tobiasdiez
Copy link
Contributor

Reviewer: Tobias Diez

@tobiasdiez
Copy link
Contributor

comment:7

Sure, it's already better than no typing at all.

Feel free to set it to "positive review" after the tests pass.

@fchapoton
Copy link
Contributor Author

comment:8

ok, thanks. For sure, only a first step.

I was wondering how to add typing information explaining that the output type has the same "Parent" as self.

@tobiasdiez
Copy link
Contributor

comment:10

Replying to @fchapoton:

I was wondering how to add typing information explaining that the output type has the same "Parent" as self.

In general you can use @overload for methods to provide a more refined typing information depending on the type of the arguments, in particular, also of self.
For example,

    @overload
    def method(self: 'MyClass', other: 'OtherClass') -> 'ReturnClass':
        pass

    def method(self: Any, other: Any) -> Any:
        ....

The category framework probably needs further work to be able to provide meaningful typing information. One way path would be to use generics. For example, sage.categories.action.Action could be declared as

G = TypeVar('G') # possibly with some constraints
S = TypeVar('S')

class Action(Generic[G, S]):

   def __init__(G: G, S: S):
       ....
   
   def actor(self) -> G:
       ....

   def codomain(self) -> S:
       ...

@fchapoton
Copy link
Contributor Author

Dependencies: #30551

@mkoeppe
Copy link
Contributor

mkoeppe commented Dec 4, 2020

comment:12

To my understanding, type annotations themselves are supported in Python 3.6.
from future import annotations is for "postponed evaluation of annotations" (https://www.python.org/dev/peps/pep-0563/)

I think the annotations added on this ticket do not need from future import annotations - so this line can be removed and the dependency can be dropped

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 5, 2020

Branch pushed to git repo; I updated commit sha1 and set ticket back to needs_review. This was a forced push. New commits:

fdadfa8add some basic typing information to Tamari interval posets

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 5, 2020

Changed commit from e1faf42 to fdadfa8

@fchapoton
Copy link
Contributor Author

comment:14

ok, here is a more basic annotation, without future import

Using python 3.8.5, I needed to change back list[tuple] to just list

please review again

@fchapoton
Copy link
Contributor Author

Changed dependencies from #30551 to none

@tobiasdiez
Copy link
Contributor

comment:15

If you don't want to use __future__, then you have to use List[Tuple[...]] from typing, and also sometimes wrap typing information in strings (e.g. so that you don't run into issues with forward references).
I'm in favor of using the future import, since this can easily be migrated once only >= python 3.9 is supported (by simply removing the import).

I think it's a good idea to specify the complete typing information, so also specify the type of the list items, i.e. List[str] instead of only List. Same for the iterator.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 5, 2020

Changed commit from fdadfa8 to e74fa70

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 5, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

e74fa70use List and Tuple

@fchapoton
Copy link
Contributor Author

comment:17

ok, here is a commit for using List[Tuple]

@tobiasdiez
Copy link
Contributor

comment:19

There are merged conflicts now.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 8, 2020

Changed commit from e74fa70 to 2272bc3

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 8, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

2272bc3Merge branch 'u/chapoton/30989' in 9.3.b3

@fchapoton
Copy link
Contributor Author

comment:21

merged

@tobiasdiez
Copy link
Contributor

comment:22

LGTM. The patchbot fails, but the errors seem not relevant to the changes on the branch.

@vbraun
Copy link
Member

vbraun commented Dec 21, 2020

Changed branch from u/chapoton/30989 to 2272bc3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants