Skip to content
This repository was archived by the owner on Sep 3, 2026. It is now read-only.
This repository was archived by the owner on Sep 3, 2026. It is now read-only.

Possible name collision for same-named source file in different directories #198

Closed

Description

@rchen152

From googleapis/google-cloud-python#6769 (comment). That particular issue hasn't been figured out yet, but it got me to think about whether it's generally possible for pytype to accidentally map two source files to the same pyi path, and it indeed is. With a structure like:

root
|----dir1
|    |----foo.py
|----dir2
     |----foo.py

when pytype is run from root, ninja errors out with: ninja: error: build.ninja:11: multiple rules generate /usr/local/google/home/rechen/root/pytype_output/pyi/foo.pyi [-w dupbuild=err]. Perhaps we need to include the relative path from the current directory to the pythonpath directory in pytype_output.

Activity

  1. martindemello commented on Dec 3, 2018

    @martindemello
    Contributor

    note that this is asking pytype to analyse two unrelated projects (that happen to be sitting in the same directory on the user's filesystem). i think we should flag this as an error (possibly with a more informative error message) rather than try to put them in the same output tree.

  2. rchen152 commented on Dec 4, 2018

    @rchen152
    ContributorAuthor

    True, we may want to forbid this rather than supporting it. Either way, the current behavior (cryptic failure) isn't good.

  3. cmarqu commented on Oct 21, 2019

    @cmarqu
  4. rchen152 commented on Oct 21, 2019

    @rchen152
    ContributorAuthor

    Hmm, in this case, since all of the packages and subpackages have __init__.py files, I'd have expected pytype to be able to use the full module names to distinguish between the two files. Which directory are you running pytype from in which there's a name collision?

  5. cmarqu commented on Oct 21, 2019

    @cmarqu

    I have tried both the very toplevel directory as well as the cocotb subdirectory. Let me boot up the machine where I tried this and see if I can figure out more.

  6. cmarqu commented on Oct 21, 2019

    @cmarqu

    For some reason, I cannot reproduce this anymore. As a workaround, I had added one of those "duplicating" directories to the exclude list, but now that I removed it again, it still works, even after removing the .pytype output directory. Please disregard, sorry for the confusion.

  7. rchen152 commented on Oct 21, 2019

    @rchen152
    ContributorAuthor

    No worries, thanks for looking into it again!

  8. mrahtz commented on Sep 5, 2020

    @mrahtz

    An example of this error even within one project is

    git clone https://github.com/hill-a/stable-baselines
    cd stable-baselines
    git checkout 3840ddf752143f7fc1a54bf3debc125855df4ca2
    patch -p0 -i patch.diff
    pytype -o /tmp/out
    pytype -P /tmp/out

    where patch.diff contains

    diff --git stable_baselines/common/base_class.py stable_baselines/common/base_class.py
    index 994a539..e26b591 100644
    --- stable_baselines/common/base_class.py
    +++ stable_baselines/common/base_class.py
    @@ -940,7 +940,7 @@ class ActorCriticRLModel(BaseRLModel):
                                  "Stored kwargs: {}, specified kwargs: {}".format(data['policy_kwargs'],
                                                                                   kwargs['policy_kwargs']))
    
    -        model = cls(policy=data["policy"], env=None, _init_setup_model=False)
    +        model = cls(policy=data["policy"], env=None, _init_setup_model=False)  # pytype: disable=not-instantiable
             model.__dict__.update(data)
             model.__dict__.update(kwargs)
             model.set_env(env)
    @@ -1048,7 +1048,7 @@ class OffPolicyRLModel(BaseRLModel):
                                  "Stored kwargs: {}, specified kwargs: {}".format(data['policy_kwargs'],
                                                                                   kwargs['policy_kwargs']))
    
    -        model = cls(policy=data["policy"], env=None, _init_setup_model=False)
    +        model = cls(policy=data["policy"], env=None, _init_setup_model=False)  # pytype: disable=not-instantiable
             model.__dict__.update(data)
             model.__dict__.update(kwargs)
             model.set_env(env)

    This gives ninja: error: build.ninja:46: multiple rules generate /__init__.pyi [-w dupbuild=err].

  9. rchen152 commented on Sep 8, 2020

    @rchen152
    ContributorAuthor

    Hmm, why are you running pytype -P /tmp/out? -P is for paths to source code directories, whereas /tmp/out looks like it contains just-generated .imports and .pyi files.

  10. mrahtz commented on Sep 13, 2020

    @mrahtz

    I might well have been doing something silly here. I was trying to run pytype in type-check mode, using previously-generated stubs.

    Looking through pytype --help, I wasn't sure what -P is supposed to do (why would you specify source code directories with -P rather than just specifying them as the input argument?), but running pytype-single --help, I saw there was a fuller description of -P, and took it to mean that it could be used to specify a directory with previously-generated stubs - and that this also applied to regular pytype. I guess this is not the case?

  11. rchen152 commented on Sep 16, 2020

    @rchen152
    ContributorAuthor

    Ah, the -P arguments to pytype and pytype-single are similar but not quite the same. They're both for telling pytype where the dependencies of your input files are, but for pytype, you specify source files whereas for pytype-single you specify type stubs. You wouldn't just pass these files as inputs because they would then be type-checked.

  12. mrahtz commented on Sep 16, 2020

    @mrahtz

    Ahh, gotcha :) Thanks!

  13. added
    cat: infrastructure
    on Jan 20, 2021
  14. nirradi commented on Oct 6, 2021

    @nirradi

    This reproduces every time for me if there's no __init__.py in the folder of one of the duplicates.

  15. lucaswiman commented on Sep 23, 2022

    @lucaswiman

    Agree with @nirradi that adding a missing __init__.py fixed the problem for me. To future visitors to this page: you may be missing __init__.py files somewhere in your packages.

    Maybe the multiple rules generate error should be extended to include a note that pytype has poor compatibility with PEP 420 implicit namespace packages and to try adding __init__.py files?

  16. rchen152 commented on Sep 27, 2022

    @rchen152
    ContributorAuthor

    Aha, knowing that adding __init__.py files fixes the problem is helpful, thanks! It sounds like the issue is probably here:

    if path_utils.exists(path_utils.join(containing_dir, "__init__.py")):
    # If the file's containing directory has an __init__.py, we assume that
    # the file is in a (sub)package. Add the containing directory of the
    # top-level package so that 'from package import module' works.
    package_parent = path_utils.dirname(containing_dir)
    while path_utils.exists(path_utils.join(package_parent, "__init__.py")):
    package_parent = path_utils.dirname(package_parent)
    p = package_parent
    . We're relying on the presence of __init__.py to find subpackages, which indeed does not work for implicit namespace packages.

  17. h-joo commented on Aug 20, 2025

    @h-joo
    Contributor

    Hi, thank you for your report. Google is shifting its effort to a different approach for type checking apart from pytype, and we're not planning to put in any effort in the near future in pytype, so we'll close this issue. Please see the announcement here.

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

    bugcat: infrastructure

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions