GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-46571: improve `typing.no_type_check` to skip foreign objects by sobolevn · Pull Request #31042 · python/cpython

Conversation

@sobolevn

@sobolevn sobolevn commented Feb 1, 2022 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There are several changes:

  1. We now don't explicitly check for any base / sub types, because new name check covers it
  2. I've also checked that no_type_check do not modify foreign functions. It was the same as with types
  3. I've also covered except TypeError in no_type_check with a simple test case, it was not covered at all
  4. I also felt like adding lambda test is a good idea: because lambda is a bit of both in class bodies: a function and an assignment

Are there any other cases we want to cover?

https://bugs.python.org/issue46571

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@sobolevn
sobolevn requested review from Fidget-Spinner and gvanrossum as code owners February 1, 2022 08:29
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Feb 1, 2022
@bedevere-bot bedevere-bot added the awaiting review label Feb 1, 2022
@AlexWaygood
AlexWaygood self-requested a review February 1, 2022 08:39
@sobolevn
bpo-46571: improve typing.no_type_check to skip foreign objects
a03009c
@sobolevn
sobolevn force-pushed the issue-46571 branch from d25557c to a03009c Compare February 1, 2022 08:54
@JelleZijlstra
JelleZijlstra self-requested a review February 1, 2022 18:32
@AlexWaygood

AlexWaygood commented Feb 1, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Here's a slightly evil corner case that this patch doesn't account for:

If I have a module typing_test.py, like so:

# typing_test.py
class A:
    class AA:
        foo: int

Then, observe the following behaviour (with your patch applied):

>>> from typing import get_type_hints, no_type_check
>>> import typing_test
>>> get_type_hints(typing_test.A.AA)
{'foo': <class 'int'>}
>>> @no_type_check
... class A:
...     AA = typing_test.A.AA
... 
...     
>>> get_type_hints(typing_test.A.AA)
{}

We might be able to get around this by looking at the __module__ attribute.

sobolevn reacted with eyes emoji
All reactions
  • 👀 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood
AlexWaygood reviewed Feb 1, 2022
View reviewed changes
Comment thread Lib/test/test_typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

JelleZijlstra
JelleZijlstra requested changes Feb 2, 2022
View reviewed changes
Comment thread Lib/test/test_typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot added awaiting core review and removed awaiting review labels Feb 2, 2022
@sobolevn

sobolevn commented Feb 2, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

@AlexWaygood good one! Very evil!

I've addressed this.

@JelleZijlstra I agree that we should also fix classmethod / staticmethod problem. Because we declare to support all methods, not just instance methods.

The problem is that types.MethodType does not support attribute assignment:

>>> class Some:
...     @staticmethod
...     def st(x: int) -> int: ...
...     @classmethod
...     def cl(cls, y: int) -> int: ...

>>> Some.st.__no_type_check__ = True

>>> Some.cl.__no_type_check__ = True
AttributeError: 'method' object has no attribute '__no_type_check__'

Ideas?

Moreover, do we need some special @property support?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood

AlexWaygood commented Feb 2, 2022

Copy link
Copy Markdown
Member

@AlexWaygood good one! Very evil!

I've addressed this.

Doesn't look like you've pushed anything -- did you mean to? :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sobolevn

sobolevn commented Feb 2, 2022

Copy link
Copy Markdown
Member Author

Doesn't look like you've pushed anything -- did you mean to? :)

Not yet, I am still fighting classmethod 🙂

AlexWaygood reacted with thumbs up emoji AlexWaygood reacted with laugh emoji
All reactions
  • 👍 1 reaction
  • 😄 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sobolevn

sobolevn commented Feb 2, 2022

Copy link
Copy Markdown
Member Author

Probably

if isinstance(obj, types.MethodType):
                obj.__func__.__no_type_check__ = True

is the way to go 🤔

Will write some more tests to be sure.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

sobolevn added 2 commits February 2, 2022 14:11
@sobolevn
Address review, add @classmethod support
964dc69
@sobolevn
Update 2022-02-01-11-21-34.bpo-46571.L40xUJ.rst
24d737e
JelleZijlstra
JelleZijlstra approved these changes Feb 17, 2022
View reviewed changes

@JelleZijlstra JelleZijlstra left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Looks good.

Do you think this should be backported? I'm leaning no as it's a behavior change and nobody directly complained about the old behavior (looks like you found it while reviewing typing test coverage).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting core review labels Feb 17, 2022
@JelleZijlstra JelleZijlstra self-assigned this Feb 17, 2022
gvanrossum
gvanrossum approved these changes Feb 17, 2022
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Agreed, a backport doesn't seem asked for.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@JelleZijlstra
JelleZijlstra merged commit 395029b into python:main Feb 19, 2022
@bedevere-bot bedevere-bot removed the awaiting merge label Feb 19, 2022
@JelleZijlstra JelleZijlstra mentioned this pull request Apr 13, 2022
Add typing to 3.11 What's New #91491
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@AlexWaygood AlexWaygood AlexWaygood left review comments
@JelleZijlstra JelleZijlstra JelleZijlstra approved these changes
@gvanrossum gvanrossum gvanrossum approved these changes
@Fidget-Spinner Fidget-Spinner Awaiting requested review from Fidget-Spinner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@JelleZijlstra JelleZijlstra

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@sobolevn @AlexWaygood @JelleZijlstra @gvanrossum @the-knights-who-say-ni @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.