Just _names_? There's a recurring error case when a 3rd-party module overrides a standard one if it happens to have the same name. If you
filter such a module out, you're shooting yourself in the foot...
That's not possible.
Stdlib can be arranged any way a user/maintainer wishes (zipped
stdlib and virtual environments are just two examples), so there's
no way to tell if the module's location is "right".
Dowstream changes are also standard practice so there's no way to
verify a module's contents, either.
As such, there's no way to tell if any given module being
imported is a standard or a 3rd-party one.
-- Regards, Ivan
On Mon, Jan 25, 2021 at 6:37 PM Chris Jerdonek <chris.j...@gmail.com> wrote:
> On Mon, Jan 25, 2021 at 7:51 AM Ivan Pozdeev via Python-Dev <pytho...@python.org> wrote:
>>
>> Just _names_? There's a recurring error case when a 3rd-party module overrides a standard one if it happens to have the same name. If you
>> filter such a module out, you're shooting yourself in the foot...
>
> Would another use case be to support issuing a warning if a third-party module is imported whose name matches a standard one? A related use case would be to build on this and define a function that accepts an already imported module and return whether it is from the standard library. Unlike, the module_names attribute, this function would reflect the reality of the underlying module, and so not have false positives as with doing a name check alone.
This is a different use case which requires a different solution.
sys.module_names solve some specific use cases (that I listed in my
first email).
In Python 3.9, you can already check if a module __file__ is in the
sysconfig.get_paths()['stdlib'] directory. You don't need to modify
Python for that.
On Tue, 26 Jan 2021 10:36:10 +1100
Steven D'Aprano <st...@pearwood.info> wrote:
> On Mon, Jan 25, 2021 at 06:17:09PM +0100, Victor Stinner wrote:
> > Hi Bernat,
> >
> > "stdlib_module_names" was my first idea but it looks too long, so I
> > chose "module_names". But someone on Twitter and now you asked me why
> > not "stdlib_module_names", so I wrote a PR to rename module_names to
> > sys.stdlib_module_names:
> > https://github.com/python/cpython/pull/24332
> >
> > At least "stdlib_module_names" better summarizes its definition: "A
> > frozenset of strings containing the names of standard library
> > modules".
>
> Your first instinct that it is too long is correct.
Disagreed. This is niche enough that it warrants a long but explicit
name, rather than some ambiguous shortcut.
On Mon, Jan 25, 2021, at 18:44, Chris Jerdonek wrote:
> But to issue a warning when a standard module is being overridden like
> I was suggesting, wouldn’t you also need to know whether the name of
> the module being imported is a standard name, which is what
> says.module_names provides?
I don't think the warning would be only useful for stdlib modules... has any thought been given to warning when a module being imported from the current directory / script directory is the same as an installed package?