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

WIP: Typed Agument Clinic #44

Closed
wants to merge 3 commits into from
Closed

WIP: Typed Agument Clinic #44

wants to merge 3 commits into from

Conversation

erlend-aasland
Copy link
Owner

No description provided.

@erlend-aasland
Copy link
Owner Author

cc. @AlexWaygood

@erlend-aasland
Copy link
Owner Author

So, I basically just ran mypy on the two .py files in Tools/clinic, and I tweaked them until mypy stopped complaining 😄

@@ -1915,8 +1916,11 @@ def dump(self):
# maps strings to Language objects.
# "languages" maps the name of the language ("C", "Python").
# "extensions" maps the file extension ("c", "py").
L = TypeVar('L', bound=Language)
LangDict = dict[str, L]
Copy link
Owner Author

Choose a reason for hiding this comment

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

I'm slightly surprised I cannot just do this:

LangDict = dict[str, Language]

Copy link

@AlexWaygood AlexWaygood May 1, 2023

Choose a reason for hiding this comment

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

What's the error you get if you do that? I was going to suggest you do that!

Copy link
Owner Author

Choose a reason for hiding this comment

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

Tools/clinic/clinic.py:1922: error: Value expression in dictionary comprehension has incompatible type "Type[CLanguage]"; expected type "Language"  [misc]
Tools/clinic/clinic.py:1923: error: Incompatible types in assignment (expression has type "Type[PythonLanguage]", target has type "Language")  [assignment]
Found 2 errors in 1 file (checked 1 source file)

Copy link

@AlexWaygood AlexWaygood May 1, 2023

Choose a reason for hiding this comment

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

Right, so it should be LangDict = dict[str, type[Language]] rather than dict[str, Language] or dict[str, L]. The values are class objects, rather than instances of the class :)

Copy link
Owner Author

Choose a reason for hiding this comment

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

Aaaaaah #til

Copy link

@AlexWaygood AlexWaygood May 1, 2023

Choose a reason for hiding this comment

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

For some heated debate on whether the type-abstract error code is useful at all, you can see python/mypy#4717

Copy link
Owner Author

Choose a reason for hiding this comment

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

Seems like this is a bug in mypy (Language is a class with an abc metaclass)

Copy link
Owner Author

Choose a reason for hiding this comment

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

I'll just revert to what I had earlier

Copy link

@AlexWaygood AlexWaygood May 1, 2023

Choose a reason for hiding this comment

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

Hmm, what you had earlier is incorrect :/

Mypy is working as intended here -- it's a question of the design choice mypy has made here rather than a bug, per se. Mypy believes that if you have a type[Foo] annotation, you'll probably want to instantiate Foo at some point, so it's therefore unsound to have a type[Foo] annotation if Foo is an abstract class, since abstract classes can't be instantiated. In this case Language is an abstract class (it has an abc metaclass and abstract methods, so it can't be instantiated). So mypy is working as designed.

However, arguably this lint is based on some flawed premises and makes life unduly complicated in lots of situations. (See python/mypy#4717 for detailed debate on this point.)

You can work around the issue by using the Callable[[str], Language] annotation I suggested above (which is just as valid as type[Language] here), or disable the error code. But using a TypeVar is incorrect here, since there's no type-dependent relationship going on.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Got it; I'll investigate tomorrow. (FYI, your comments did not show up until now.)

Tools/clinic/clinic.py Outdated Show resolved Hide resolved
Tools/clinic/clinic.py Outdated Show resolved Hide resolved
Tools/clinic/clinic.py Outdated Show resolved Hide resolved
Tools/clinic/cpp.py Outdated Show resolved Hide resolved
@AlexWaygood
Copy link

By the way, some stricter mypy command-line options that I recommend enabling right from the beginning are: --disallow-any-generics, --warn-unused-ignores, --warn-redundant-casts and --enable-error-code="ignore-without-code". These will help you get into good habits and save you pain later on :)

(These can all be enabled via a config file as well as via command-line arguments. I usually use a pyproject.toml file, but a mypy.ini file can also be used.)

- ditch no-return
- use callable from collections.abc
- use namedtuple from typing
@erlend-aasland
Copy link
Owner Author

Great, thanks for your help and patience so far :)

@erlend-aasland
Copy link
Owner Author

CC. @hauntsaninja

@AlexWaygood
Copy link

AlexWaygood commented May 4, 2023

FYI, I currently have python/pyperformance#286 open to add a basic mypy check to pyperformance — could be useful as a template for what the GitHub Actions workflow and config file might look like!

@erlend-aasland
Copy link
Owner Author

FYI, I currently have python/pyperformance#286 open to add a basic mypy check to pyperformance — could be useful as a template for what the GitHub Actions workflow and config file might look like!

Oh, that's neat! Thanks for the heads-up. We should start with something similar over at the CPython repo.

@erlend-aasland erlend-aasland deleted the clinic-typed branch May 16, 2023 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants