MCPcopy Index your code
hub / github.com/python/mypy / init_under_package_root

Method init_under_package_root

mypy/fscache.py:82–137  ·  view source on GitHub ↗

Is this path an __init__.py under a package root? This is used to detect packages that don't contain __init__.py files, which is needed to support Bazel. The function should only be called for non-existing files. It will return True if it refers to a __init__.py fi

(self, path: str)

Source from the content-addressed store, hash-verified

80 return st
81
82 def init_under_package_root(self, path: str) -> bool:
83 """Is this path an __init__.py under a package root?
84
85 This is used to detect packages that don't contain __init__.py
86 files, which is needed to support Bazel. The function should
87 only be called for non-existing files.
88
89 It will return True if it refers to a __init__.py file that
90 Bazel would create, so that at runtime Python would think the
91 directory containing it is a package. For this to work you
92 must pass one or more package roots using the --package-root
93 flag.
94
95 As an exceptional case, any directory that is a package root
96 itself will not be considered to contain a __init__.py file.
97 This is different from the rules Bazel itself applies, but is
98 necessary for mypy to properly distinguish packages from other
99 directories.
100
101 See https://docs.bazel.build/versions/master/be/python.html,
102 where this behavior is described under legacy_create_init.
103 """
104 if not self.package_root:
105 return False
106 dirname, basename = os.path.split(path)
107 if basename != "__init__.py":
108 return False
109 if not os.path.basename(dirname).isidentifier():
110 # Can't put an __init__.py in a place that's not an identifier
111 return False
112
113 st = self.stat_or_none(dirname)
114 if st is None:
115 return False
116 else:
117 if not stat.S_ISDIR(st.st_mode):
118 return False
119 ok = False
120
121 # skip if on a different drive
122 current_drive, _ = os.path.splitdrive(os.getcwd())
123 drive, _ = os.path.splitdrive(path)
124 if drive != current_drive:
125 return False
126 if os.path.isabs(path):
127 path = os.path.relpath(path)
128 path = os.path.normpath(path)
129 for root in self.package_root:
130 if path.startswith(root):
131 if path == root + basename:
132 # A package root itself is never a package.
133 ok = False
134 break
135 else:
136 ok = True
137 return ok
138
139 def _fake_init(self, path: str) -> os.stat_result:

Callers 2

stat_or_noneMethod · 0.95
get_init_fileMethod · 0.45

Calls 3

stat_or_noneMethod · 0.95
splitMethod · 0.80
startswithMethod · 0.45

Tested by

no test coverage detected