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

Make "python -m inspect <name>" meaningful #62826

ncoghlan opened this issue Aug 2, 2013 · 7 comments

Make "python -m inspect <name>" meaningful #62826

ncoghlan opened this issue Aug 2, 2013 · 7 comments
stdlib Python modules in the Lib dir type-feature A feature request or enhancement


Copy link

ncoghlan commented Aug 2, 2013

BPO 18626
Nosy @ncoghlan, @ezio-melotti, @PCManticore, @ericsnowcurrently, @serhiy-storchaka
  • inspect.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2013-09-22.12:47:02.910>
    created_at = <Date 2013-08-02.03:12:40.405>
    labels = ['type-feature', 'library']
    title = 'Make "python -m inspect <name>" meaningful'
    updated_at = <Date 2013-09-22.12:49:39.541>
    user = '' fields:

    activity = <Date 2013-09-22.12:49:39.541>
    actor = 'ncoghlan'
    assignee = 'none'
    closed = True
    closed_date = <Date 2013-09-22.12:47:02.910>
    closer = 'python-dev'
    components = ['Library (Lib)']
    creation = <Date 2013-08-02.03:12:40.405>
    creator = 'ncoghlan'
    dependencies = []
    files = ['31799']
    hgrepos = []
    issue_num = 18626
    keywords = ['patch']
    message_count = 7.0
    messages = ['194148', '194153', '194699', '195478', '197919', '198272', '198273']
    nosy_count = 6.0
    nosy_names = ['ncoghlan', 'ezio.melotti', 'Claudiu.Popa', 'python-dev', 'eric.snow', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = ''
    versions = ['Python 3.4']

    Copy link
    Contributor Author

    ncoghlan commented Aug 2, 2013

    "python -m inspect <name>" doesn't currently do anything.

    It would be handy if this:

    python -m inspect site

    Was roughly equivalent to:

    python -c "import inspect, site; print(inspect.getsource(site))"

    Even better would be if it understood entry point notation so you could use "modname:qualname" to get the source code of a particular item within a module.

    @ncoghlan ncoghlan added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Aug 2, 2013
    Copy link

    At the risk of unnecessary complication, there is also other information that could be output, depending on the referenced object (module vs. class/func via qualname). For instance, a module's __file__ would be handy. So, the output could have a "header" with the relevant info, followed by a blank line, and then the source code.

    I suppose something like that could be added later with a commandline option, like "python -m inspect --info site", rather than being default behavior. Then this issue can stay focused on the simpler idea. :)

    Regardless, your proposal sounds good to me. I don't see any other meaningful use for "-m inspect".

    Copy link

    I agree that it will be better to output a formalized report about a module and it's contents using inspect.getmoduleinfo() and inspect.getmembers().

    Copy link
    Contributor Author

    I realised that with the "module:qualname" syntax, it's straightforward to expand this beyond module introspection to arbitrary objects.

    What I suggest we could output:

    • a header with key module info (names taken from PEP-451):
      Submodule search path
      Loader (repr output)

    • for non-module objects, also include the line within the file (if it can be determined)

    • the output of getsource() if a "--source" option is given

    @ncoghlan ncoghlan changed the title Make "python -m inspect <name>" dump the source of a module Make "python -m inspect <name>" meaningful Aug 17, 2013
    Copy link

    PCManticore mannequin commented Sep 16, 2013

    Hello, here's a basic patch. Currently, the header info is printed by default, while the source can be retrieved by using --source (although I would prefer them to be switched, the source should be shown by default and the header info only when requested). It understands the entry point notation, like 'unittest:util.safe_repr'.

    Copy link

    python-dev mannequin commented Sep 22, 2013

    New changeset 2e1335245f8f by Nick Coghlan in branch 'default':
    Close bpo-18626: add a basic CLI for the inspect module

    @python-dev python-dev mannequin closed this as completed Sep 22, 2013
    Copy link
    Contributor Author

    Thanks for the initial patch Claudiu - I tweaked it a bit before committing it.

    • as you suggested, displaying the source is the default, with a --details option to display the formatted info instead
    • changed the displayed details (e.g. only displaying the search path for packages, displaying the line number for classes and functions
    • a few tweaks to the actual implementation (e.g. using partition over find, avoiding tracebacks for a couple of likely user errors)

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    None yet

    No branches or pull requests

    3 participants