1+ from __future__ import annotations
2+
13import argparse
24import os
35import pytest
46import re
7+ from typing import TYPE_CHECKING
58
69import pytest_codecov .git as git
710import pytest_codecov .codecov as codecov
811
12+ if TYPE_CHECKING :
13+ from coverage import Coverage
14+ from pytest_cov .plugin import CovPlugin # type: ignore[import-untyped]
15+
916
1017__version__ = '0.7.0'
1118token_regex = re .compile (
1623)
1724
1825
19- def validate_token (arg ) :
26+ def validate_token (arg : str ) -> str :
2027 if not token_regex .match (arg ):
2128 msg = 'Invalid Codecov.io repository token supplied.'
2229 raise argparse .ArgumentTypeError (msg )
2330 return arg
2431
2532
26- def validate_slug (arg ) :
33+ def validate_slug (arg : str ) -> str :
2734 if not slug_regex .match (arg ):
2835 msg = 'Invalid repository slug supplied.'
2936 raise argparse .ArgumentTypeError (msg )
3037 return arg
3138
3239
33- def pytest_addoption (parser , pluginmanager ):
40+ def pytest_addoption (
41+ parser : pytest .Parser ,
42+ pluginmanager : pytest .PytestPluginManager
43+ ) -> None :
3444 group = parser .getgroup ('codecov' )
3545 group .addoption (
3646 '--codecov' ,
@@ -83,20 +93,25 @@ def pytest_addoption(parser, pluginmanager):
8393 action = 'store_false' ,
8494 dest = 'codecov_upload_on_failure' ,
8595 default = True ,
86- help = ' Don\ ' t upload coverage results on test failure'
96+ help = " Don't upload coverage results on test failure"
8797 )
8898 group .addoption (
8999 '--codecov-exclude-junit-xml' ,
90100 action = 'store_false' ,
91101 dest = 'codecov_junit_xml' ,
92102 default = True ,
93- help = ' Don\ ' t upload the junit xml file'
103+ help = " Don't upload the junit xml file"
94104 )
95105
96106
97107class CodecovPlugin :
98108
99- def upload_report (self , terminalreporter , config , cov ):
109+ def upload_report (
110+ self ,
111+ terminalreporter : pytest .TerminalReporter ,
112+ config : pytest .Config ,
113+ cov : Coverage
114+ ) -> None :
100115 option = config .option
101116 uploader = codecov .CodecovUploader (
102117 option .codecov_slug ,
@@ -105,7 +120,7 @@ def upload_report(self, terminalreporter, config, cov):
105120 token = option .codecov_token ,
106121 )
107122 uploader .add_network_files (git .ls_files ())
108- from coverage .misc import CoverageException
123+ from coverage .exceptions import CoverageException
109124 try :
110125 uploader .add_coverage_report (cov )
111126 except CoverageException as exc :
@@ -188,12 +203,20 @@ def upload_report(self, terminalreporter, config, cov):
188203 terminalreporter .write_line (f'ERROR: { error } ' , red = True , bold = True )
189204
190205 @pytest .hookimpl (trylast = True )
191- def pytest_terminal_summary (self , terminalreporter , exitstatus , config ):
192- cov_plugin = config .pluginmanager .get_plugin ('_cov' )
206+ def pytest_terminal_summary (
207+ self ,
208+ terminalreporter : pytest .TerminalReporter ,
209+ exitstatus : int ,
210+ config : pytest .Config
211+ ) -> None :
212+ cov_plugin : CovPlugin | None = config .pluginmanager .get_plugin ('_cov' )
213+ if cov_plugin is None :
214+ return
215+
193216 if cov_plugin .cov_controller is None :
194217 return
195218
196- cov = cov_plugin .cov_controller .cov
219+ cov : Coverage | None = cov_plugin .cov_controller .cov
197220 if cov is None :
198221 return
199222
@@ -203,7 +226,7 @@ def pytest_terminal_summary(self, terminalreporter, exitstatus, config):
203226 self .upload_report (terminalreporter , config , cov )
204227
205228
206- def pytest_configure (config ) : # pragma: no cover
229+ def pytest_configure (config : pytest . Config ) -> None : # pragma: no cover
207230 # NOTE: Don't report codecov results on worker nodes
208231 if hasattr (config , 'workerinput' ):
209232 return
0 commit comments