Skip to content

Commit

Permalink
Add --scripts-are-modules flag. Fixes python#1380. (python#2138)
Browse files Browse the repository at this point in the history

If set, each script file passed on the command line (i.e. without .py extension) is considered a module by that name (its basename, actually). Without this flag, the script is considered to be module __main__ and there can be only one.
  • Loading branch information
gvanrossum authored Sep 15, 2016
1 parent d7f6ea6 commit fccb062
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 4 additions & 1 deletion mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ def process_options(args: List[str],
help="dump type inference stats")
parser.add_argument('--custom-typing', metavar='MODULE', dest='custom_typing_module',
help="use a custom typing module")
parser.add_argument('--scripts-are-modules', action='store_true',
help="Script x becomes module x instead of __main__")
# hidden options
# --shadow-file a.py tmp.py will typecheck tmp.py in place of a.py.
# Useful for tools to make transformations to a file to get more
Expand Down Expand Up @@ -319,7 +321,8 @@ def process_options(args: List[str],
.format(f))
targets.extend(sub_targets)
else:
targets.append(BuildSource(f, None, None))
mod = os.path.basename(f) if options.scripts_are_modules else None
targets.append(BuildSource(f, mod, None))
return targets, options


Expand Down
3 changes: 3 additions & 0 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def __init__(self) -> None:
# Files in which to allow strict-Optional related errors
self.strict_optional_whitelist = None # type: Optional[List[str]]

# Use script name instead of __main__
self.scripts_are_modules = False

# -- development options --
self.verbosity = 0 # More verbose messages (for troubleshooting)
self.pdb = False
Expand Down
17 changes: 17 additions & 0 deletions test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -1131,3 +1131,20 @@ reveal_type(x)
[out]
main:1: error: Incompatible types in assignment (expression has type "int", variable has type "str")
main:2: error: Revealed type is 'builtins.str'

-- Scripts and __main__

[case testScriptsAreModules]
# flags: --scripts-are-modules
[file a]
pass
[file b]
pass

[case testScriptsAreNotModules]
# cmd: mypy a b
[file a]
pass
[file b]
pass
[out]

0 comments on commit fccb062

Please sign in to comment.