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 — `inspect.getsource` (`findsource`) not working as expected when duplicate class is used · Issue #106727 · python/cpython

inspect.getsource (findsource) not working as expected when duplicate class is used #106727

New issue
Copy link
New issue
Copy link
Closed
Closed
inspect.getsource (findsource) not working as expected when duplicate class is used#106727
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@Viicos
Viicos
opened on Jul 13, 2023
Issue body actions

Bug report

Consider the following example:

import inspect

class Model:
    a = 1

class Model:
    a = 2

print(Model.a)
#> 2
print(inspect.getsource(Model))
#> class Model:
#>     a = 1

I think we should expect the last class to be used (it works as expected if we use functions instead of classes).

This issue was raised in an external project:

  • inspect.getsourcelines is wrong octoml/synr#13

And I'm facing this issue while working on:

  • Extract attribute docstrings for FieldInfo.description pydantic/pydantic#6563

Let me know if this should be fixed, I'll take a deeper look at what was done in #10307 and see if I can make a PR

Your environment

  • CPython versions tested on: 3.11.4
  • Operating system and architecture: Ubuntu

Linked PRs

  • gh-106727: Make inspect.getsource smarter for class for same name definitions #106815
  • gh-106727: Add __module__ check for inspect.getsource(cls) #106968

Activity

  1. Viicos
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jul 13, 2023
  2. Viicos
    changed the title [-]`inspect.getsource` not working as expected when duplicate class is used[/-] [+]`inspect.getsource` (`findsource`) not working as expected when duplicate class is used[/+] on Jul 13, 2023
  3. AlexWaygood
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Jul 13, 2023
  4. gaogaotiantian commented on Jul 13, 2023

    @gaogaotiantian
    gaogaotiantian
    on Jul 13, 2023
    Member
    More actions

    The method has already been changed from regex to ast. I can work on the PR if you are okay with it. If you want to fix this yourself, the code piece is at the similar place, just switched to an ast based searching.

  5. gaogaotiantian commented on Jul 13, 2023

    @gaogaotiantian
    gaogaotiantian
    on Jul 13, 2023
    Member
    More actions

    Just BTW, the last class may not be the one you need.

    if cond:
        class A:
            ...
    else:
        class A:
            ...

    We could probably need some heuristics to figure out which class it should be.

  6. Viicos commented on Jul 14, 2023

    @Viicos
    Viicos
    on Jul 14, 2023
    ContributorAuthor
    More actions

    The method has already been changed from regex to ast. I can work on the PR if you are okay with it. If you want to fix this yourself, the code piece is at the similar place, just switched to an ast based searching.

    Yes by look at what was done in #10307 I meant improve this ast based search.

    We could probably need some heuristics to figure out which class it should be.

    I wonder if this is possible depending of the condition evaluated.

  7. gaogaotiantian commented on Jul 14, 2023

    @gaogaotiantian
    gaogaotiantian
    on Jul 14, 2023
    Member
    More actions

    Oh sorry I did not look at the PR thoroughly. I saw the word "regex" and thought it was improving the regex method.

    classes do not have useful compile time information with it as far as I know which makes this tricky - that's why I said we probably need some heuristics. I doubt if we can make this work:

    if runtime_cond:
        class A:
            pass
    else:
        class A:
            pass

    They are indistinguishable. We can, however, try to figure out which class it is by some "guessing" like reading their methods. Let me know if this is out of your comfortable zone and you'd like me to work on it, I'm kind of familiar with the area. Otherwise, let me know if you have any question about the implementation.

  8. Viicos commented on Jul 15, 2023

    @Viicos
    Viicos
    on Jul 15, 2023
    ContributorAuthor
    More actions

    I'll try to take a look, I'm not that familiar with ast but I'll try to come up with something (this is a good opportunity to learn more about it), we will then be able to work on this together if I encounter any blocking points

  9. Viicos commented on Jul 16, 2023

    @Viicos
    Viicos
    on Jul 16, 2023
    ContributorAuthor
    More actions

    Actually while I see how this could be implemented, I don't really see which attributes should be used to guess the right class. @gaogaotiantian if you have something in mind I'll be pleased to see a PR

  10. gaogaotiantian commented on Jul 16, 2023

    @gaogaotiantian
    gaogaotiantian
    on Jul 16, 2023
    Member
    More actions

    No worries, I'll work on it, it can be a bit tricky.

  11. Viicos commented on Jul 16, 2023

    @Viicos
    Viicos
    on Jul 16, 2023
    ContributorAuthor
    More actions

    No worries, I'll work on it, it can be a bit tricky.

    Yeah I was first thinking maybe use the values from __dict__, but we have to make sure not to include attributes set by a potential metaclass

  12. carljm
    added 2 commits that reference this issue on Jul 18, 2023

    gh-106727: Make `inspect.getsource` smarter for class for same name d…

    663854d

    gh-106727: Add `__module__` check for `inspect.getsource(cls)` (#106968)

    b383703
  13. jtcave
    added a commit that references this issue on Jul 27, 2023

    pythongh-106727: Add `__module__` check for `inspect.getsource(cls)` (p…

    3801021
  14. Viicos
    closed this as completedon Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.