Skip to content

Commit 76fe36a

Browse files
authored
Merge pull request #1618 from markshannon/python-refine-module-names
Python refine module naming.
2 parents 85a0566 + 90475fa commit 76fe36a

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

python/ql/src/semmle/python/Module.qll

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -211,33 +211,34 @@ private predicate legalShortName(string name) {
211211
*/
212212
private predicate isPotentialSourcePackage(Folder f) {
213213
f.getRelativePath() != "" and
214-
(
215-
exists(f.getFile("__init__.py"))
216-
or
217-
py_flags_versioned("options.respect_init", "False", _) and major_version() = 2
218-
)
214+
isPotentialPackage(f)
215+
}
216+
217+
private predicate isPotentialPackage(Folder f) {
218+
exists(f.getFile("__init__.py"))
219+
or
220+
py_flags_versioned("options.respect_init", "False", _) and major_version() = 2
219221
}
220222

221223
private string moduleNameFromBase(Container file) {
222-
file instanceof Folder and result = file.getBaseName()
224+
isPotentialPackage(file) and result = file.getBaseName()
223225
or
224226
file instanceof File and result = file.getStem()
225227
}
226228

227229
string moduleNameFromFile(Container file) {
228230
exists(string basename |
229231
basename = moduleNameFromBase(file) and
230-
legalShortName(basename)
231-
|
232+
legalShortName(basename) and
232233
result = moduleNameFromFile(file.getParent()) + "." + basename
233-
or
234-
isPotentialSourcePackage(file) and result = file.getStem() and
235-
(not isPotentialSourcePackage(file.getParent()) or not legalShortName(file.getParent().getBaseName()))
236-
or
237-
result = file.getStem() and file.getParent() = file.getImportRoot()
238-
or
239-
result = file.getStem() and isStubRoot(file.getParent())
240234
)
235+
or
236+
isPotentialSourcePackage(file) and result = file.getStem() and
237+
(not isPotentialSourcePackage(file.getParent()) or not legalShortName(file.getParent().getBaseName()))
238+
or
239+
result = file.getStem() and file.getParent() = file.getImportRoot()
240+
or
241+
result = file.getStem() and isStubRoot(file.getParent())
241242
}
242243

243244
private predicate isStubRoot(Folder f) {

0 commit comments

Comments
 (0)