Skip to content

Commit

Permalink
chore: upgraded Ruff linter (#18188)
Browse files Browse the repository at this point in the history
* Upgraded Ruff linter

* Formatted whole codebase with new ruff rules

* Revert import removal

* Fixed mypi issues or added ignores

* Fixed schema formatting

* Fixed hogvm failing tests

* Remove duplicate key in list
  • Loading branch information
Gilbert09 authored Oct 26, 2023
1 parent 6cc24ba commit 5513be7
Show file tree
Hide file tree
Showing 1,075 changed files with 41,013 additions and 9,775 deletions.
25 changes: 19 additions & 6 deletions ee/api/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

@api_view(["GET"])
def saml_metadata_view(request, *args, **kwargs):

if (
not request.user.organization_memberships.get(organization=request.user.organization).level
>= OrganizationMembership.Level.ADMIN
Expand All @@ -46,7 +45,6 @@ class MultitenantSAMLAuth(SAMLAuth):
"""

def get_idp(self, organization_domain_or_id: Union["OrganizationDomain", str]):

try:
organization_domain = (
organization_domain_or_id
Expand All @@ -57,7 +55,10 @@ def get_idp(self, organization_domain_or_id: Union["OrganizationDomain", str]):
raise AuthFailed("saml", "Authentication request is invalid. Invalid RelayState.")

if not organization_domain.organization.is_feature_available(AvailableFeature.SAML):
raise AuthFailed("saml", "Your organization does not have the required license to use SAML.")
raise AuthFailed(
"saml",
"Your organization does not have the required license to use SAML.",
)

return SAMLIdentityProvider(
str(organization_domain.id),
Expand Down Expand Up @@ -88,7 +89,12 @@ def auth_url(self):
# name, since we multiple IdPs share the same auth_complete URL.
return auth.login(return_to=str(instance.id))

def _get_attr(self, response_attributes: Dict[str, Any], attribute_names: List[str], optional: bool = False) -> str:
def _get_attr(
self,
response_attributes: Dict[str, Any],
attribute_names: List[str],
optional: bool = False,
) -> str:
"""
Fetches a specific attribute from the SAML response, attempting with multiple different attribute names.
We attempt multiple attribute names to make it easier for admins to configure SAML (less configuration to set).
Expand All @@ -114,7 +120,9 @@ def get_user_details(self, response):
attributes = response["attributes"]
return {
"fullname": self._get_attr(
attributes, ["full_name", "FULL_NAME", "fullName", OID_COMMON_NAME], optional=True
attributes,
["full_name", "FULL_NAME", "fullName", OID_COMMON_NAME],
optional=True,
),
"first_name": self._get_attr(
attributes,
Expand All @@ -140,7 +148,12 @@ def get_user_details(self, response):
),
"email": self._get_attr(
attributes,
["email", "EMAIL", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress", OID_MAIL],
[
"email",
"EMAIL",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress",
OID_MAIL,
],
),
}

Expand Down
9 changes: 7 additions & 2 deletions ee/api/billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ def patch(self, request: Request, *args: Any, **kwargs: Any) -> Response:
BillingManager(license).update_billing(org, {"custom_limits_usd": custom_limits_usd})

if distinct_id:
posthoganalytics.capture(distinct_id, "billing limits updated", properties={**custom_limits_usd})
posthoganalytics.capture(
distinct_id,
"billing limits updated",
properties={**custom_limits_usd},
)
posthoganalytics.group_identify(
"organization",
str(org.id),
Expand Down Expand Up @@ -143,7 +147,8 @@ def license(self, request: Request, *args: Any, **kwargs: Any) -> HttpResponse:
license = License(key=serializer.validated_data["license"])

res = requests.get(
f"{BILLING_SERVICE_URL}/api/billing", headers=BillingManager(license).get_auth_headers(organization)
f"{BILLING_SERVICE_URL}/api/billing",
headers=BillingManager(license).get_auth_headers(organization),
)

if res.status_code != 200:
Expand Down
6 changes: 5 additions & 1 deletion ee/api/dashboard_collaborator.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ class DashboardCollaboratorViewSet(
mixins.DestroyModelMixin,
viewsets.GenericViewSet,
):
permission_classes = [IsAuthenticated, TeamMemberAccessPermission, CanEditDashboardCollaborator]
permission_classes = [
IsAuthenticated,
TeamMemberAccessPermission,
CanEditDashboardCollaborator,
]
pagination_class = None
queryset = DashboardPrivilege.objects.select_related("dashboard").filter(user__is_active=True)
lookup_field = "user__uuid"
Expand Down
6 changes: 5 additions & 1 deletion ee/api/ee_event_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
from ee.models.event_definition import EnterpriseEventDefinition
from posthog.api.shared import UserBasicSerializer
from posthog.api.tagged_item import TaggedItemSerializerMixin
from posthog.models.activity_logging.activity_log import dict_changes_between, log_activity, Detail
from posthog.models.activity_logging.activity_log import (
dict_changes_between,
log_activity,
Detail,
)


class EnterpriseEventDefinitionSerializer(TaggedItemSerializerMixin, serializers.ModelSerializer):
Expand Down
6 changes: 5 additions & 1 deletion ee/api/ee_property_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
from posthog.api.shared import UserBasicSerializer
from posthog.api.tagged_item import TaggedItemSerializerMixin
from posthog.models import PropertyDefinition
from posthog.models.activity_logging.activity_log import dict_changes_between, log_activity, Detail
from posthog.models.activity_logging.activity_log import (
dict_changes_between,
log_activity,
Detail,
)


class EnterprisePropertyDefinitionSerializer(TaggedItemSerializerMixin, serializers.ModelSerializer):
Expand Down
15 changes: 12 additions & 3 deletions ee/api/explicit_team_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,24 @@ class Meta:
"user_uuid", # write_only (see above)
"effective_level", # read_only (calculated)
]
read_only_fields = ["id", "parent_membership_id", "joined_at", "updated_at", "user", "effective_level"]
read_only_fields = [
"id",
"parent_membership_id",
"joined_at",
"updated_at",
"user",
"effective_level",
]

def create(self, validated_data):
team: Team = self.context["get_team"]()
user_uuid = validated_data.pop("user_uuid")
validated_data["team"] = team
try:
requesting_parent_membership: OrganizationMembership = OrganizationMembership.objects.get(
organization_id=team.organization_id, user__uuid=user_uuid, user__is_active=True
requesting_parent_membership: (OrganizationMembership) = OrganizationMembership.objects.get(
organization_id=team.organization_id,
user__uuid=user_uuid,
user__is_active=True,
)
except OrganizationMembership.DoesNotExist:
raise exceptions.PermissionDenied("You both need to belong to the same organization.")
Expand Down
4 changes: 3 additions & 1 deletion ee/api/feature_flag_role_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def has_permission(self, request, view):
)
if resource_access.access_level == OrganizationResourceAccess.AccessLevel.CAN_ALWAYS_EDIT:
return True
except OrganizationResourceAccess.DoesNotExist: # no organization resource access for this means full default edit access
except (
OrganizationResourceAccess.DoesNotExist
): # no organization resource access for this means full default edit access
return True
try:
feature_flag: FeatureFlag = FeatureFlag.objects.get(id=view.parents_query_dict["feature_flag_id"])
Expand Down
11 changes: 9 additions & 2 deletions ee/api/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
from ee.models.hook import Hook
from posthog.api.routing import StructuredViewSetMixin
from posthog.models.user import User
from posthog.permissions import OrganizationMemberPermissions, TeamMemberAccessPermission
from posthog.permissions import (
OrganizationMemberPermissions,
TeamMemberAccessPermission,
)


class HookSerializer(serializers.ModelSerializer):
Expand All @@ -35,7 +38,11 @@ class HookViewSet(StructuredViewSetMixin, viewsets.ModelViewSet):

queryset = Hook.objects.all()
ordering = "-created_at"
permission_classes = [IsAuthenticated, OrganizationMemberPermissions, TeamMemberAccessPermission]
permission_classes = [
IsAuthenticated,
OrganizationMemberPermissions,
TeamMemberAccessPermission,
]
serializer_class = HookSerializer

def perform_create(self, serializer):
Expand Down
6 changes: 5 additions & 1 deletion ee/api/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

from ee.tasks.slack import handle_slack_event
from posthog.api.integration import IntegrationSerializer
from posthog.models.integration import Integration, SlackIntegration, SlackIntegrationError
from posthog.models.integration import (
Integration,
SlackIntegration,
SlackIntegrationError,
)


class PublicIntegrationViewSet(viewsets.GenericViewSet):
Expand Down
10 changes: 9 additions & 1 deletion ee/api/organization_resource_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@
class OrganizationResourceAccessSerializer(serializers.ModelSerializer):
class Meta:
model = OrganizationResourceAccess
fields = ["id", "resource", "access_level", "organization", "created_at", "updated_at", "created_by"]
fields = [
"id",
"resource",
"access_level",
"organization",
"created_at",
"updated_at",
"created_by",
]
read_only_fields = ["id", "created_at", "created_by", "organization"]

def validate_resource(self, resource):
Expand Down
15 changes: 12 additions & 3 deletions ee/api/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class RolePermissions(BasePermission):
def has_permission(self, request, view):
organization = request.user.organization

requesting_membership: OrganizationMembership = OrganizationMembership.objects.get(
requesting_membership: (OrganizationMembership) = OrganizationMembership.objects.get(
user_id=cast(User, request.user).id,
organization=organization,
)
Expand All @@ -42,7 +42,15 @@ class RoleSerializer(serializers.ModelSerializer):

class Meta:
model = Role
fields = ["id", "name", "feature_flags_access_level", "created_at", "created_by", "members", "associated_flags"]
fields = [
"id",
"name",
"feature_flags_access_level",
"created_at",
"created_by",
"members",
"associated_flags",
]
read_only_fields = ["id", "created_at", "created_by"]

def validate_name(self, name):
Expand All @@ -55,7 +63,8 @@ def create(self, validated_data):
validated_data["organization"] = organization
try:
default_flags_org_setting = OrganizationResourceAccess.objects.get(
organization=organization, resource=OrganizationResourceAccess.Resources.FEATURE_FLAGS
organization=organization,
resource=OrganizationResourceAccess.Resources.FEATURE_FLAGS,
).access_level
except OrganizationResourceAccess.DoesNotExist:
default_flags_org_setting = OrganizationResourceAccess.AccessLevel.CAN_ALWAYS_EDIT
Expand Down
3 changes: 0 additions & 3 deletions ee/api/sentry_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def get_sentry_stats(start_time: str, end_time: str) -> Tuple[dict, int]:
def get_tagged_issues_stats(
start_time: str, end_time: str, tags: Dict[str, str], target_issues: List[str]
) -> Dict[str, Any]:

sentry_config: Dict[str, str] = get_instance_settings(["SENTRY_AUTH_TOKEN", "SENTRY_ORGANIZATION"])

org_slug = sentry_config.get("SENTRY_ORGANIZATION")
Expand Down Expand Up @@ -92,7 +91,6 @@ def get_stats_for_timerange(
target_end_time: str,
tags: Optional[Dict[str, str]] = None,
) -> Tuple[int, int]:

base_counts, base_total_count = get_sentry_stats(base_start_time, base_end_time)
target_counts, target_total_count = get_sentry_stats(target_start_time, target_end_time)

Expand All @@ -101,7 +99,6 @@ def get_stats_for_timerange(

@api_view(["GET"])
def sentry_stats(request: HttpRequest):

try:
current_time = datetime.utcnow()
target_end_date = current_time.strftime("%Y-%m-%dT%H:%M:%S")
Expand Down
7 changes: 6 additions & 1 deletion ee/api/test/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ def license_required_response(
self,
message: str = "This feature is part of the premium PostHog offering. Self-hosted licenses are no longer available for purchase. Please contact [email protected] to discuss options.",
) -> Dict[str, Optional[str]]:
return {"type": "server_error", "code": "payment_required", "detail": message, "attr": None}
return {
"type": "server_error",
"code": "payment_required",
"detail": message,
"attr": None,
}

@classmethod
def setUpTestData(cls):
Expand Down
27 changes: 20 additions & 7 deletions ee/api/test/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def test_create_action_update_delete_tags(self):
from ee.models.license import License, LicenseManager

super(LicenseManager, cast(LicenseManager, License.objects)).create(
key="key_123", plan="enterprise", valid_until=timezone.datetime(2038, 1, 19, 3, 14, 7)
key="key_123",
plan="enterprise",
valid_until=timezone.datetime(2038, 1, 19, 3, 14, 7),
)

response = self.client.post(f"/api/projects/{self.team.id}/actions/", data={"name": "user signed up"})
Expand All @@ -32,7 +34,8 @@ def test_create_action_update_delete_tags(self):
self.assertEqual(set(response.json()["tags"]), {"hello", "random"})

response = self.client.patch(
f"/api/projects/{self.team.id}/actions/{response.json()['id']}", data={"name": "user signed up", "tags": []}
f"/api/projects/{self.team.id}/actions/{response.json()['id']}",
data={"name": "user signed up", "tags": []},
)

self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand All @@ -42,12 +45,17 @@ def test_create_action_with_tags(self):
from ee.models.license import License, LicenseManager

super(LicenseManager, cast(LicenseManager, License.objects)).create(
key="key_123", plan="enterprise", valid_until=timezone.datetime(2038, 1, 19, 3, 14, 7)
key="key_123",
plan="enterprise",
valid_until=timezone.datetime(2038, 1, 19, 3, 14, 7),
)

response = self.client.post(
f"/api/projects/{self.team.id}/actions/",
data={"name": "user signed up", "tags": ["nightly", "is", "a", "good", "girl"]},
data={
"name": "user signed up",
"tags": ["nightly", "is", "a", "good", "girl"],
},
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(set(response.json()["tags"]), {"nightly", "is", "a", "good", "girl"})
Expand All @@ -56,7 +64,9 @@ def test_actions_does_not_nplus1(self):
from ee.models.license import License, LicenseManager

super(LicenseManager, cast(LicenseManager, License.objects)).create(
key="key_123", plan="enterprise", valid_until=timezone.datetime(2038, 1, 19, 3, 14, 7)
key="key_123",
plan="enterprise",
valid_until=timezone.datetime(2038, 1, 19, 3, 14, 7),
)

# Ensure the cloud check is cached to not affect the number of queries
Expand All @@ -79,10 +89,13 @@ def test_actions_no_duplicate_tags(self):
from ee.models.license import License, LicenseManager

super(LicenseManager, cast(LicenseManager, License.objects)).create(
key="key_123", plan="enterprise", valid_until=timezone.datetime(2038, 1, 19, 3, 14, 7)
key="key_123",
plan="enterprise",
valid_until=timezone.datetime(2038, 1, 19, 3, 14, 7),
)
response = self.client.post(
f"/api/projects/{self.team.id}/actions/", data={"name": "user signed up", "tags": ["a", "b", "a"]}
f"/api/projects/{self.team.id}/actions/",
data={"name": "user signed up", "tags": ["a", "b", "a"]},
)

self.assertListEqual(sorted(response.json()["tags"]), ["a", "b"])
Loading

0 comments on commit 5513be7

Please sign in to comment.