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

Static Typing profilers/utils.py #630

Merged
merged 13 commits into from
Sep 21, 2022

Conversation

tonywu315
Copy link
Contributor

No description provided.

@CLAassistant
Copy link

CLAassistant commented Sep 15, 2022

CLA assistant check
All committers have signed the CLA.

@taylorfturner taylorfturner added Work In Progress Solution is being developed static_typing mypy static typing issues labels Sep 15, 2022
@tonywu315
Copy link
Contributor Author

#608

@JGSweets JGSweets enabled auto-merge (squash) September 15, 2022 16:12
@JGSweets JGSweets changed the title Static Typing profilers/utils.py [WIP] Static Typing profilers/utils.py Sep 15, 2022
auto-merge was automatically disabled September 19, 2022 03:18

Head branch was pushed to by a user without write access

@tonywu315 tonywu315 changed the title [WIP] Static Typing profilers/utils.py Static Typing profilers/utils.py Sep 19, 2022
@taylorfturner taylorfturner removed the Work In Progress Solution is being developed label Sep 19, 2022
@JGSweets
Copy link
Contributor

@tonywu315 FYI, I think your code is out of date. You will likely need to rebase this branch onto C1/main.

Comment on lines 494 to 499
if stat1 is None or stat2 is None:
return [i if i is None else i.strftime("%x %X") for i in [stat1, stat2]]
elif stat1 != stat2:
diff = stat1 - stat2
return ("+" if diff.days >= 0 else "-") + str(abs(diff))
return "unchanged"
Copy link
Contributor

Choose a reason for hiding this comment

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

any reason for removing the
diff = find_diff_of_numbers(stat1, stat2)
and handling this here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

find_diff_of_numbers() only takes in ints and floats. If I make it also take in datetime, then mypy complains because ints/floats can't be subtracted with datetime.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair, should we fix numbers to take in any type that can have a subtraction operation? Not sure if that's actually feasible. Otherwise, can do as is but fix the above.

Copy link
Contributor

Choose a reason for hiding this comment

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

Found this:
https://stackoverflow.com/questions/37669222/how-can-i-hint-that-a-type-is-comparable-with-typing

from __future__ import annotations

from abc import abstractmethod
from typing import MutableSequence, Protocol, TypeVar


class Comparable(Protocol):
    """Protocol for annotating comparable types."""

    @abstractmethod
    def __lt__(self: CT, other: CT) -> bool:
        pass


CT = TypeVar("CT", bound=Comparable)

def comparison_sort(s: MutableSequence[CT]) -> None:
    pass


comparison_sort([1, 2, 3])  # OK
comparison_sort([1.0, 2.0, 3.0])  # OK
comparison_sort(["42", "420", "2137"])  # OK
comparison_sort([1, 2, "3"])  # mypy error 

Could we alter Comparable name and abstractions to use __eq__ and __sub__. which would allow a datetime to go into diff_of_numbers?

Maybe that's not appropriate either bc it would take in anything with that.

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 added this and it seems to work fine. The only thing is that I need to make __sub__ return Any because subtracting datetime results in a timedelta.

Copy link
Contributor

@JGSweets JGSweets left a comment

Choose a reason for hiding this comment

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

Fix to diff_of_dates / test.

T = TypeVar("T", bound=Subtractable)


def find_diff_of_numbers(stat1: Optional[T], stat2: Optional[T]) -> Any:
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that's fine for now, but in the log run we should try to avoid using Any where possible.

Ideally we could create a typing file we use that makes these descriptive declarations. As things like any or Optional[T] don't make sense to someone reading it without diving deep into the code.

Will approve though, a future state for which to strive.

@micdavis micdavis merged commit 22c40e5 into capitalone:main Sep 21, 2022
@tonywu315 tonywu315 deleted the static_typing/profilers/utils branch September 21, 2022 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
static_typing mypy static typing issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants