Skip to content

Commit

Permalink
Merge pull request lyft#41 from lyft/fix_mypy
Browse files Browse the repository at this point in the history
Fix mypy type annotations
  • Loading branch information
asottile authored Mar 13, 2019
2 parents d977b80 + 8af8d08 commit 732d69b
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ htmlcov/
.tox/
.coverage
.coverage.*
.cache
/.mypy_cache
nosetests.xml
coverage.xml
*,cover
Expand Down
15 changes: 10 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,31 @@ repos:
- id: check-ast
- id: check-docstring-first
- id: check-executables-have-shebangs
- id: check-json
- id: check-merge-conflict
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.7
hooks:
- id: flake8
additional_dependencies:
- flake8>=3.6.0,<4
- flake8-bugbear
- flake8-builtins
- flake8-comprehensions
- flake8-commas
- id: trailing-whitespace
- repo: https://github.com/asottile/yesqa
rev: v0.0.8
rev: v0.0.10
hooks:
- id: yesqa
additional_dependencies:
- flake8>=3.6.0,<4
- flake8==3.7.7
- flake8-bugbear
- flake8-builtins
- flake8-comprehensions
- flake8-commas
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.670
hooks:
- id: mypy
5 changes: 4 additions & 1 deletion xiblint/rules/accessibility_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
view_accessibility_identifier,
)

if False: # if TYPE_CHECKING:
from xiblint.xibcontext import XibContext


class AccessibilityFormat(Rule):
"""
Checks for incorrect use of Lyft extensions `accessibilityFormat` and `accessibilitySources`.
"""
def check(self, context): # type: (Rule, xiblint.xibcontext.XibContext) -> None
def check(self, context): # type: (XibContext) -> None
views_with_accessibility_format = {
element.parent.parent
for element in context.tree.findall(
Expand Down
5 changes: 4 additions & 1 deletion xiblint/rules/accessibility_labels_for_image_buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
get_view_user_defined_attr,
)

if False: # if TYPE_CHECKING:
from xiblint.xibcontext import XibContext


class AccessibilityLabelsForImageButtons(Rule):
"""
Checks for image buttons with no accessibility label.
In this case, VoiceOver will announce the image asset's name, which might be unwanted.
"""
def check(self, context): # type: (Rule, xiblint.xibcontext.XibContext) -> None
def check(self, context): # type: (XibContext) -> None
for button in context.tree.findall(".//button"):
state_normal = button.find("./state[@key='normal']")
if (
Expand Down
5 changes: 4 additions & 1 deletion xiblint/rules/accessibility_labels_for_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
get_view_user_defined_attr,
)

if False: # if TYPE_CHECKING:
from xiblint.xibcontext import XibContext


class AccessibilityLabelsForImages(Rule):
"""
Checks for accessible images with no accessibility label.
In this case, VoiceOver will announce the image asset's name, which might be unwanted.
"""
def check(self, context): # type: (Rule, xiblint.xibcontext.XibContext) -> None
def check(self, context): # type: (XibContext) -> None
for image_view in context.tree.findall(".//imageView"):
if (
view_is_accessibility_element(image_view) is True and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
view_is_accessibility_element,
)

if False: # if TYPE_CHECKING:
from xiblint.xibcontext import XibContext


class AccessibilityLabelsForTextWithPlaceholder(Rule):
"""
Checks for text fields and text views with a placeholder and no accessibility label.
A placeholder is not a substitute for an accessibility label, since it's no longer announced
after the text is edited.
"""
def check(self, context): # type: (Rule, xiblint.xibcontext.XibContext) -> None
def check(self, context): # type: (XibContext) -> None
for element in context.tree.findall(".//textField") + context.tree.findall(".//textView"):
placeholder = (element.get('placeholder') if element.tag == 'textField'
else get_view_user_defined_attr(element, 'placeholder'))
Expand Down
5 changes: 4 additions & 1 deletion xiblint/rules/autolayout_frames.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from xiblint.rules import Rule

if False: # if TYPE_CHECKING:
from xiblint.xibcontext import XibContext


class AutolayoutFrames(Rule):
"""
Checks for ambiguous and misplaced views.
"""
def check(self, context): # type: (Rule, xiblint.xibcontext.XibContext) -> None
def check(self, context): # type: (XibContext) -> None
for element in context.tree.iter():
if element.get('ambiguous') == 'YES':
context.error(element, "View with ambiguous constraints")
Expand Down
5 changes: 4 additions & 1 deletion xiblint/rules/automation_identifiers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from xiblint.rules import Rule

if False: # if TYPE_CHECKING:
from xiblint.xibcontext import XibContext


class AutomationIdentifiers(Rule):
"""
Makes sure that interactive views have accessibility identifiers, to support testing through UI Automation.
"""
def check(self, context): # type: (Rule, xiblint.xibcontext.XibContext) -> None
def check(self, context): # type: (XibContext) -> None
for tag in ['button', 'textField', 'textView']:
for view in context.tree.findall(".//{}".format(tag)):
if view.find('./accessibility[@identifier]') is None:
Expand Down
5 changes: 4 additions & 1 deletion xiblint/rules/automation_identifiers_for_outlet_labels.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from xiblint.rules import Rule

if False: # if TYPE_CHECKING:
from xiblint.xibcontext import XibContext


class AutomationIdentifiersForOutletLabels(Rule):
"""
Checks for labels with outlets into a view controller that have no accessibility identifiers.
Labels with outlets might get dynamic text, and therefore should be accessible to UI testing.
"""
def check(self, context): # type: (Rule, xiblint.xibcontext.XibContext) -> None
def check(self, context): # type: (XibContext) -> None
for outlet in context.tree.findall(".//viewController/connections/outlet"):
destination = outlet.get('destination')
label = context.tree.find(".//label[@id='{}']".format(destination))
Expand Down
5 changes: 4 additions & 1 deletion xiblint/rules/no_simulated_metrics.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from xiblint.rules import Rule

if False: # if TYPE_CHECKING:
from xiblint.xibcontext import XibContext


class NoSimulatedMetrics(Rule):
"""
Ensures there are no `simulatedMetricsContainer`s, which were removed with Xcode 9
"""
def check(self, context): # type: (Rule, xiblint.xibcontext.XibContext) -> None
def check(self, context): # type: (XibContext) -> None
root = context.tree.getroot()
for container in root.findall('.//simulatedMetricsContainer'):
context.error(container, 'SimulatedMetricsContainers should be removed (resave with Xcode 9+)')
5 changes: 4 additions & 1 deletion xiblint/rules/no_trait_variations.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from xiblint.rules import Rule

if False: # if TYPE_CHECKING:
from xiblint.xibcontext import XibContext


class NoTraitVariations(Rule):
"""
Checks for Trait Variations being enabled.
"""
def check(self, context): # type: (Rule, xiblint.xibcontext.XibContext) -> None
def check(self, context): # type: (XibContext) -> None
root = context.tree.getroot()
if root.get('useTraitCollections') == 'YES' and root.get('targetRuntime') != 'watchKit':
context.error(root, "Document has 'Use Trait Variations' enabled")
5 changes: 4 additions & 1 deletion xiblint/rules/no_view_controller_links_to_other_bundles.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from xiblint.rules import Rule

if False: # if TYPE_CHECKING:
from xiblint.xibcontext import XibContext


class NoViewControllerLinksToOtherBundles(Rule):
"""
Ensures there are no links to storyboards in different bundles
"""
def check(self, context): # type: (Rule, xiblint.xibcontext.XibContext) -> None
def check(self, context): # type: (XibContext) -> None
for element in context.tree.findall(".//viewControllerPlaceholder"):
if element.get("bundleIdentifier"):
context.error(element, "Linking to a view controller in another bundle")
5 changes: 4 additions & 1 deletion xiblint/rules/simulated_metrics_retina4_0.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from xiblint.rules import Rule

if False: # if TYPE_CHECKING:
from xiblint.xibcontext import XibContext

ALLOWED_DEVICES = [
'retina4_0',
'watch38',
Expand All @@ -11,7 +14,7 @@ class SimulatedMetricsRetina40(Rule):
Ensures simulated metrics are for the iPhone SE or a 38mm watch
which are currently the smallest display profiles.
"""
def check(self, context): # type: (Rule, xiblint.xibcontext.XibContext) -> None
def check(self, context): # type: (XibContext) -> None
root = context.tree.getroot()

# In Xcode 9 this metadata is in a new place
Expand Down
5 changes: 4 additions & 1 deletion xiblint/rules/strict_font_names.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from xiblint.rules import Rule

if False: # if TYPE_CHECKING:
from xiblint.xibcontext import XibContext


class StrictFontNames(Rule):
"""
Expand All @@ -11,7 +14,7 @@ class StrictFontNames(Rule):
"allow_system_fonts": true
}
"""
def check(self, context): # type: (Rule, xiblint.xibcontext.XibContext) -> None
def check(self, context): # type: (XibContext) -> None
allowed_fonts = self.config.get('allowed_fonts', [])
allow_system_fonts = self.config.get('allow_system_fonts', False)

Expand Down
7 changes: 6 additions & 1 deletion xiblint/rules/unavailable_custom_classes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from xml.etree.ElementTree import Element

from xiblint.rules import Rule

if False: # if TYPE_CHECKING:
from xiblint.xibcontext import XibContext


class UnavailableCustomClasses(Rule):
"""
Expand All @@ -14,7 +19,7 @@ class UnavailableCustomClasses(Rule):
}
}
"""
def check(self, context): # type: (Rule, xiblint.xibcontext.XibContext) -> None
def check(self, context): # type: (XibContext) -> None
unavailable_classes = self.config.get('custom_classes', {})

for element in context.tree.findall('.//*[@customClass]'):
Expand Down
7 changes: 6 additions & 1 deletion xiblint/rules/unavailable_system_classes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from xml.etree.ElementTree import Element

from xiblint.rules import Rule

if False: # if TYPE_CHECKING:
from xiblint.xibcontext import XibContext


class UnavailableSystemClasses(Rule):
"""
Expand All @@ -15,7 +20,7 @@ class UnavailableSystemClasses(Rule):
}
}
"""
def check(self, context): # type: (Rule, xiblint.xibcontext.XibContext) -> None
def check(self, context): # type: (XibContext) -> None
custom_classes = self.config.get('system_classes', {})

for tag_name in custom_classes.keys():
Expand Down
6 changes: 3 additions & 3 deletions xiblint/xmlutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import sys

assert 'xml.etree.ElementTree' not in sys.modules
sys.modules['_elementtree'] = {}
sys.modules['_elementtree'] = None # type: ignore

from xml.etree import ElementTree # noqa E402
from defusedxml.ElementTree import DefusedXMLParser # noqa E402
from xml.etree import ElementTree # noqa: E402
from defusedxml.ElementTree import DefusedXMLParser # noqa: E402


class _TreeBuilder(ElementTree.TreeBuilder):
Expand Down

0 comments on commit 732d69b

Please sign in to comment.