Skip to content

Replace Python2 type comments with type annotations#870

Merged
jklaise merged 2 commits intoSeldonIO:masterfrom
mauicv:feature/remove-type-comments
Feb 13, 2023
Merged

Replace Python2 type comments with type annotations#870
jklaise merged 2 commits intoSeldonIO:masterfrom
mauicv:feature/remove-type-comments

Conversation

@mauicv
Copy link
Contributor

@mauicv mauicv commented Feb 1, 2023

What is this:

Removes python2 type comments with type annotations, fixes #863. Required before we can merge #825.

@mauicv mauicv requested a review from ascillitoe February 1, 2023 18:02
@codecov
Copy link

codecov bot commented Feb 1, 2023

Codecov Report

Merging #870 (a7edce9) into master (980563b) will increase coverage by 0.01%.
The diff coverage is 98.16%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #870      +/-   ##
==========================================
+ Coverage   85.28%   85.29%   +0.01%     
==========================================
  Files          74       74              
  Lines        8759     8768       +9     
==========================================
+ Hits         7470     7479       +9     
  Misses       1289     1289              
Flag Coverage Δ
macos-latest-3.10 85.25% <98.16%> (+0.01%) ⬆️
ubuntu-latest-3.10 85.25% <98.16%> (+0.01%) ⬆️
ubuntu-latest-3.7 85.16% <98.16%> (+0.01%) ⬆️
ubuntu-latest-3.8 85.09% <98.16%> (-0.08%) ⬇️
ubuntu-latest-3.9 85.18% <98.16%> (+0.01%) ⬆️
windows-latest-3.9 82.92% <98.16%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...i/explainers/anchors/anchor_tabular_distributed.py 27.18% <0.00%> (ø)
alibi/explainers/cfproto.py 75.57% <88.88%> (+0.03%) ⬆️
alibi/api/defaults.py 100.00% <100.00%> (ø)
alibi/api/interfaces.py 77.65% <100.00%> (ø)
alibi/confidence/trustscore.py 86.07% <100.00%> (ø)
alibi/explainers/anchors/anchor_base.py 92.75% <100.00%> (+0.05%) ⬆️
alibi/explainers/anchors/anchor_image.py 93.12% <100.00%> (ø)
alibi/explainers/anchors/anchor_tabular.py 89.57% <100.00%> (ø)
alibi/explainers/anchors/anchor_text.py 94.44% <100.00%> (ø)
.../explainers/anchors/language_model_text_sampler.py 78.82% <100.00%> (+0.25%) ⬆️
... and 10 more


sample_stats, pos, total = [], (), () # type: List, Tuple, Tuple
sample_stats: List = []
pos: Tuple = tuple()
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the tuple() preferred to ()?

Copy link
Contributor

Choose a reason for hiding this comment

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

Just noticed we tend to mostly do tuple() elsewhere...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tend to use tuple because () is ambiguous and can create errors in certain contexts.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice idea!

# define head, tail part of the text
self.head, self.tail = '', '' # type: str, str
self.head_tokens, self.tail_tokens = [], [] # type: List[str], List[str]
self.head: str = ''
Copy link
Contributor

@ascillitoe ascillitoe Feb 6, 2023

Choose a reason for hiding this comment

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

Since this crops up in a few places, we probably need to decide on a convention for handling these single-line multi-definition bits with type annotations. We could do something like:

self.head: str = ""; self.tail: str = ""

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm happy with the above. This PEP suggest placing the type hints before the multiple assignment like so:

self.head: str
self.tail: str
self.head, self.tail = "", ""

Copy link
Contributor

@ascillitoe ascillitoe Feb 6, 2023

Choose a reason for hiding this comment

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

I sort of think what you posted above is more readable. Although it doesn't save as many lines and I still don't love it!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I prefer self.head: str = ""; self.tail: str = "" tbh.

Copy link
Contributor

Choose a reason for hiding this comment

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

@mauicv @jklaise what was the decision with this? Just to leave as separate lines?

Copy link
Contributor

Choose a reason for hiding this comment

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

Didn't seem to be very important, separate lines is nice as corresponds closely to how you might define types in a dataclass.


# combine abdm and mvdm
self.d_abs = {} # type: Dict
self.d_abs: Any = {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the change from Dict to Any?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A mistake! Good catch! Thanks


d_pair = {} # type: Dict
X_cat_eq = {} # type: Dict
d_pair: dict = {}
Copy link
Contributor

@ascillitoe ascillitoe Feb 6, 2023

Choose a reason for hiding this comment

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

Just noting that elsewhere we've done dict -> Dict but here it is Dict -> dict

Copy link
Contributor

@ascillitoe ascillitoe left a comment

Choose a reason for hiding this comment

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

A few comments but generally LGTM!

@jklaise jklaise merged commit 4665d81 into SeldonIO:master Feb 13, 2023
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.

Replace Python 2 type comments with type annotations

3 participants