- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 2.9k
Disallow Any on public interface types #6815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Closed
      
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -46,8 +46,57 @@ | |
| from _pytest.warning_types import PytestUnknownMarkWarning | ||
| from _pytest.warning_types import PytestWarning | ||
|  | ||
| # For mypy Any type checking purposes. | ||
| # This file sets disallow_any_expr to ensure that the public API | ||
| # does not have dynamic typing via Any. Manually using each public API | ||
| # type as an expression to enforce this. | ||
| __version__ = __version__ | ||
| register_assert_rewrite = register_assert_rewrite | ||
| _setup_collect_fakemodule = _setup_collect_fakemodule | ||
| cmdline = cmdline | ||
| ExitCode = ExitCode | ||
| # hookimpl = hookimpl | ||
| # hookspec = hookspec | ||
| main = main | ||
| UsageError = UsageError | ||
| __pytestPDB = __pytestPDB | ||
| _fillfuncargs = _fillfuncargs | ||
| fixture = fixture | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
 | ||
| yield_fixture = yield_fixture | ||
| freeze_includes = freeze_includes | ||
| Session = Session | ||
| mark = mark | ||
| param = param | ||
| Collector = Collector | ||
| File = File | ||
| Item = Item | ||
| exit = exit | ||
| fail = fail | ||
| importorskip = importorskip | ||
| skip = skip | ||
| xfail = xfail | ||
| Class = Class | ||
| Function = Function | ||
| Instance = Instance | ||
| Module = Module | ||
| Package = Package | ||
| approx = approx | ||
| raises = raises | ||
| deprecated_call = deprecated_call | ||
| warns = warns | ||
| PytestAssertRewriteWarning = PytestAssertRewriteWarning | ||
| PytestCacheWarning = PytestCacheWarning | ||
| PytestCollectionWarning = PytestCollectionWarning | ||
| PytestConfigWarning = PytestConfigWarning | ||
| PytestDeprecationWarning = PytestDeprecationWarning | ||
| PytestExperimentalApiWarning = PytestExperimentalApiWarning | ||
| PytestUnhandledCoroutineWarning = PytestUnhandledCoroutineWarning | ||
| PytestUnknownMarkWarning = PytestUnknownMarkWarning | ||
| PytestWarning = PytestWarning | ||
|  | ||
| set_trace = __pytestPDB.set_trace | ||
|  | ||
| # Allow set_trace() to be typed with None | ||
| set_trace = __pytestPDB.set_trace # type: ignore | ||
|  | ||
| __all__ = [ | ||
| "__version__", | ||
|  | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be moved to
__init__.pyi?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can explicitly put types into an init.pyi, but that seems less desirable than having the code annotated inline. With the current setup, the code is annotated inline, and the
disallow_any_exprensures thatAnyis not used as an annotation in this file.disallow_any_exprwas implemented into mypy here python/mypy#3519Notably, it requires that each item exists in the file as an expression. The import itself does not count as an expr here - so we need to have a series of
a = astyle expression assigmentsUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case I'd prefer to simply not use
disallow_any_exprfor now; while I dislike this part of the patch the rest is fantastic and I'd like to merge it soon. We can revisit the more controversial part later 🙂There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nipunn1313 I've meant to move the "Manually using each public API type as an expression to enforce this." part of it. It is only boilerplate, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi - sorry for the delay - I was on vacation.
Unfortunately - I do not believe we will get any protection unless we use the variables as expressions within the file. The imports themselves are not type checked or protected - only when used as expressions.
If we use a .pyi file - then the annotations in the .pyi file override the annotations in the actual .py file - and we will lose annotation coverage from the actual code - which is the purpose of #3342
py.typedstrategyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
disallow_any_expris actually the most important piece - as it's the one that is guaranteeing that the public interface is typed.The public interface is only the
__init__.pyfile - so without this boilerplate - we're not actually adding much value.